When are we permitted to multiply or divide both sides of an equation by a variable? As it is said in the mathematics books (at least the one I have), we are not permitted to divide or multiply both sides of an equation by a variable, because it is possible to lose some answers. For example, in the following equation
$$x^2=x$$
if we divide both sides by $x$, we would have $x = 1$, but the original equation has two answers, $0$ and $1$, and we've lost $x = 0$ by dividing it by a variable.
But in the same book, in order to solve a rational equation, the author multiplies both sides of the equation by $x(x-2)$, solving the equation:
$$\begin{align*}
\frac {x+2}{x-2} - \frac1x &= \frac{2}{x(x-2)}\\\\
x(x-2)\frac {x+2}{x-2} - x(x-2)\frac 1x &= x(x-2)\frac{2}{x(x-2)}\\\\
x(x+2)-(x-2)&=2\\\\
x^2+x&=0\\\\
x(x+1)&=0\\\\
x&=0\\
x&=-1
\end{align*}$$
Can someone please explain when we are allowed to do this and when we are not? I got a bit confused!

When dividing by any quantity,
or when canceling out two quantities in a ratio
(for example, canceling $x$ and $x$ to find that $\frac xx=1$),
you need to be aware of what assumptions you have to make so
that the division or canceling makes sense,
and remember that those assumptions apply to any results you get.
For example, with
$$x^2 = x,$$
$x \neq 0$ then you can divide by $x$.
One way to keep track of your assumptions is to work out different
"cases" of the solution:
Case $x = 0$: Then the equation becomes $0^2 = 0$, which is true,
so $x = 0$ is a solution.
Case $x \neq 0$: Then since $x \neq 0$, you can divide by $x$,
so $x^2 = x$ implies $x = 1$. This is consistent with the assumptions
(namely that $x\neq 0$), so $x = 1$ is a solution.
If you look at all possible cases (and here, since $x$ either is or isn't zero,
we have already covered all possible cases),
the complete solution set consists of all solutions you find in all those cases;
that is, for this problem the solution set is $x=\{0,1\}$
(in other words, $x=0$ or $x=1$).
Now consider the example from the book:
$$\frac {x+2}{x-2} - \frac1x = \frac{2}{x(x-2)}.$$
Two equal quantities multiplied by the same quantity
are two equal quantities (even if we multiply both by zero!),
so we know that
$$x(x-2)\frac {x+2}{x-2} - x(x-2)\frac 1x = x(x-2)\frac{2}{x(x-2)}.$$
The tricky part is what comes next. It looks like the multiplier $x-2$
on the right-hand side should cancel the divisor $x-2$,
that is, $\frac {x-2}{x-2} = 1.$ But this is true only if $x-2 \neq 0$;
if $x - 2 = 0$ then $\frac {x-2}{x-2} = \frac 00,$ which is undefined.
Similarly, we can only cancel $x$ and $x$ on the right-hand side if $x \neq 0$.
So again we have two cases:
Case $x - 2 = 0$ or $x = 0$: In this case, the term on the right-hand
side of the equation evaluates to $\frac 20$, which is undefined,
so there are no solutions in this case.
Case $x - 2 \neq 0$ and $x \neq 0$:
In this case we can cancel $x-2$ with $x-2$ and cancel $x$ with $x$,
so we have
$$\begin{eqnarray}
x(x+2) - (x-2) &=& 2.\\
x^2 + x &=& 0.\\
x(x+1) &=& 0.
\end{eqnarray}$$
Now, remembering that we are still working out the case where
$x - 2 \neq 0$ and $x \neq 0$, we can divide both sides by $x$:
$$\begin{eqnarray}
x+1 &=& 0. \\
x &=& -1.
\end{eqnarray}$$
So $x = -1$ is the only solution in this case.
(Alternatively, if we used $x(x+1)=0$ to conclude that "$x=0$ or $x+1=0$",
we would still be working under the assumption that $x\neq 0$,
and from those facts we could conclude simply that $x+1=0$.)
Since there were no solutions from the other case, 
$x = -1$ is altogether the only solution of the equation.
Note that this method did not identify $x=0$ as a solution.
That's because I do not accept that it is a solution:
if you set $x=0$ in $\frac {x+2}{x-2} - \frac1x = \frac{2}{x(x-2)},$
you get a term of $\frac 10$ on the left and you get $\frac 20$
on the right, and both of those are undefined.
I hope that (eventually) the book agrees with this.
