I came across a use case where I needed to disable users without actually deleting them so a user could continue to be associated with their data and reinstated at a later date.
At first I thought that I’d need to roll my own Identity Provider but I came up with a method that is slightly easier to implement.
I added a BoolCol to the auto-generated TurboGears User Class:
Class User(SQLObject):
...
disabled = BoolCol(default=False)
...
I then changed the classmethod that SOProvider uses to get the user from the database to raise an SQLObjectNotFound when a user’s “deleted” flag is set to True.
@classmethod
def by_user_name(self, text):
try:
u = self.by_email_address(text)
except SQLObjectNotFound:
raise
if u.disabled:
raise SQLObjectNotFound
else:
return u
Note that this classmethod has also been modified to return users by email address.
This way user can effectively be disabled as they can no longer log in successfully. It’s cheating outrageously because raising an SQLObjectNotFound exception is clearly lying to the SOProvider, but it works effectively.
Matthias Urlichs | 07-Sep-06 at 3:32 pm | Permalink
Why write eight lines, when four do the same thing and are (IMHO) much more readable?
Splee | 07-Sep-06 at 4:04 pm | Permalink
Because I want to be certain that the exception bubbles up to the SOProvider if the
self.by_email_address()call raises anSQLObjectNotFound.It may still work your way but I\’m not certain of it. I will test it out later today.
I can axe one line already by removing the
else:clause but the readability increase is negligible.Damjan | 08-Sep-06 at 9:00 pm | Permalink
How about just changing the users password with adding a illegal character to it. The if you need to enable the user remove the character.
Splee | 11-Sep-06 at 2:28 pm | Permalink
That works fine as long as there\’s no encryption algorithm set in your
app.cfg. As soon as you decide you want to store passwords in a more secure manner your method of disabling the user is non-reversable and not the desired behaviour for this scenario in particular.