On 11/13/06, Stuart Clarke <[EMAIL PROTECTED]> wrote:
>
> I'm wanting to put some logic in my app which will get executed every
> time a user logs in.  Specifically, I want to increment a user's login
> counter (a column in the user table), and I want to redirect them to
> certain pages, based on what group they're a member of.
>
> I can't figure out how to do this with Identity.  It sets up a login
> controller in my project, but that only seems to be called after
> successful login if the user actually went to the login page to begin
> with, not if they were attempting to visit another page, and then got
> hi-jacked because they hadn't logged in.
>
> Is there any way for me to add this logic?

Hi Stuart,

You can do this by implementing a custom identity-controller. You need
to implement a small interface and provide an entry point in your egg.

Here's a simple example, basically this is just a copy of the std.
identity provider with the configurable class names hard-coded
(softproof is the name of the project this is taken from).

import logging
log = logging.getLogger("softproof.model.identity")

class SoftproofIdentityProvider(object):

    def __init__(self):
        pass

    def create_provider_model(self):
        return

    def validate_identity(self, user_name, password, visit_key):
        user = session.query(User).get_by(user_name=user_name)
        if not user:
            log.warning("No such user: %s", user_name)
            return None
        if not self.validate_password(user, user_name, password):
            log.info("Passwords don't match for user: %s", user_name)
            return None

        log.info("associationg user (%s) with visit (%s)",
user.user_name, visit_key)
        link = session.query(VisitIdentity).get_by(visit_key=visit_key)
        if not link:
            link = VisitIdentity(visit_key=visit_key, user_id=user.user_id)
            session.save(link)
        else:
            link.user_id = user.user_id
        session.flush()
        return SoftproofIdentity(visit_key, user)

    def validate_password(self, user, user_name, password):
        return user.password == password

    def load_identity(self, visit_key):
        return SoftproofIdentity(visit_key)

    def anonymous_identity(self):
        return SoftproofIdentity(None)


class SoftproofIdentity(object):

    def __init__(self, visit_key, user=None):
        if user:
            self._user = user
        self.visit_key = visit_key
        self.key = None

    @property
    def user(self):
        try:
            return self._user
        except AttributeError:
            pass
        visit = session.query(VisitIdentity).get_by(visit_key=self.visit_key)
        if not visit:
            self._user = None
            return None
        self._user = session.query(User).get(visit.user_id)
        return self._user


    @property
    def user_name(self):
        if not self.user:
            return None
        return self.user.user_name

    @property
    def anonymous(self):
        return not self.user

    @property
    def permissions(self):
        try:
            return self._permissions
        except AttributeError:
            pass
        if not self.user:
            self._permissions = frozenset()
        else:
            self._permissions = frozenset([p.permission_name for p in
self.user.permissions])
        return self._permissions

    @property
    def groups(self):
        try:
            return self._groups
        except AttributeError:
            pass
        if not self.user:
            self._groups = frozenset()
        else:
            self._groups = frozenset([g.group_name for g in self.user.groups])
        return self._groups

    def logout(self):
        if not self.visit_key:
            return
        try:
            visit =
session.query(VisitIdentity).get_by(visit_key=self.visit_key)
            session.delete(visit)
            anon = SoftproofIdentity(None)
            identity.set_current_identity(anon)
        except:
            pass
        else:
            session.flush()

To enable this you need to add this to your call to setup() in setup.py:
    entry_points = """
    [turbogears.identity.provider]
    softproof = softproof.model:SoftproofIdentityProvider
    """,

and change this line in your to your app.cfg:
identity.provider='softproof'

HTH,
Arnar

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to