[Grok-dev] Remember who added what

Sebastian Ware sebastian at urbantalk.se
Fri Jun 13 08:23:16 EDT 2008


I am just winging it... but I have done it the simple way. I think  
there is a more grok way, but this works for me :)

-create a user class that I can model any way I like
-keep all the users in the same container
-reference users by their id (self.__name__) (login in this case)
-create a corresponding principal
   pau = getUtility(IAuthentication)
   principals = pau['principals']
   principals[login] = InternalPrincipal(login, data['password'],  
data['title'])

When I create objects I store the creator id like this:

   def __init__(self, creator_id):
     self._creator_id = creator_id

Since the currently logged in user is available through  
self.request.principal.id on a view it is easy.

I then index creator_id so I can query based on creator.

I have my user objects associated to a workflow (hurry.workflow) and  
when they are published I add roles to the principal and when they are  
unpublished I remove the roles. This way I can control access without  
having to remove the user object.

@grok.subscribe(IProtonUser, IWorkflowTransitionEvent)
def update_roles(obj, evt):
     user = IProtonUser(obj)
     pau = getUtility(IAuthentication)
     principals = pau['principals']

     role_manager = IPrincipalRoleManager(grok.getSite())
     if user.role == 'Manager':
         if IWorkflowState(user).getState() == PUBLISHED:
             role_manager.assignRoleToPrincipal('protoncms.manager',
                                                 principals.prefix +  
user.__name__)
         else:
             role_manager.removeRoleFromPrincipal('protoncms.manager',
                                                   principals.prefix +  
user.__name__)


Mvh Sebastian

13 jun 2008 kl. 13.35 skrev Peter Bengtsson:

> I've looked at the wonderful LoginDemo grokapp and it looks like the
> right thing for me to use.
> However, how do I "merge" this with my models. I want each object (eg.
> Comment object) to be "owned" by a user.
> In Django you do it this way::
>
> from django.contrib.auth.models import User
>
>
> class Comment(models.Model):
>    user = models.ForeignKey(User, unique=False)
>    title = models.CharField(max_length=100, db_index=True)
>    ...
>
>
> What's the Grok way of doing this?
>
> -- 
> Peter Bengtsson,
> work www.fry-it.com
> home www.peterbe.com
> hobby www.issuetrackerproduct.com
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev



More information about the Grok-dev mailing list