[Catalyst] Users hierarchy

jakac jakac at iprom.si
Wed Jun 4 11:53:25 BST 2008


I also discovered DBIx::Class::Tree::AdjacencyList
Using this module I can define my parent_id in my Users.pm schema like:

  __PACKAGE__->parent_column('parent_id');


And then I can get all children of one user like:

my $user_id = 7;
my $thisuser = $c->model('MyDB::Users')->find({ user_id=> $user_id });
my $children = $thisuser->children();

But this gives me only a list of children one level under $thisuser and
it's the same
as executing only:

my $children = $c->model('MyDB::Users')->find({ parent_id=> $user_id });

So it's no big deal.. Still this module is OK because it maintains
consistency of the
tree when you delete one branch for example, but I still haven't found
the solution I
need - listing all children in all levels under one parent as well as
checking if
a single user is a (grand)parent who can edit a certain user...

When listing all children it would also be necessary if I could specify
order (e.g. order by username)
and also limit and offset for pagination on website...



jakac wrote:
> Hi,
>
> Ok, I understand the concept of walking through this DB::User model
> but another
> question is - where should I put this function and how should I call
> it from my controllers?
> Until now I only used basic integrated functions that are integrated
> in catalyst & its plugins
> so I am not familiar with all the concepts behind catalyst custom models.
>
> Thanx!
>
> sindharta_tanuwijaya at yahoo.co.jp wrote:
>> Hi,
>>
>> Haven't tried it yet, but I think the code in Catalyst should look like:
>>
>> my $edited = $c->model('DB::User')->find({id=>$target_user_id});
>> my $ancestor = $c->model('DB::User')->find({id=>$edited->{parent_id}});
>> my $found = 0;
>> while ($found==false && $ancestor) {
>> if ($ancestor == $c.user.user_id)
>> $found= true;
>> $ancestor = $c->model('DB::User')->find({id=>$edited->{parent_id}});
>> }
>> return $found;
>>
>> You can push the ancestors into a list if you want to have a list of
>> users who can edit $target_user_id. But I am not really sure if you
>> can make a top-to-bottom approach with this one. (I assume
>> top-to-bottom means searching from all ancestors instead of the user
>> that you want to edit)
>>
>> Sindharta
>>
>>
>> *jakac <jakac at iprom.si>* wrote:
>>
>>
>>     Yes this seems fine but how to code this in Catalyst? :)
>>     And another thing - if I want to list users that certain
>>     this_user can edit,
>>     I would need to go through the whole list and check for each user if
>>     "this_user" is one of the (grand)parents.
>>
>>     Your approach is bottom-to-top and I now I am also looking for
>>     top-to-bottom...
>>     I think I can manage to write one of these functions by myself if
>>     someone
>>     would just give me an example how to write one of these..
>>
>>     sindharta_tanuwijaya at yahoo.co.jp wrote:
>>>     Hi,
>>>
>>>     Maybe an algorithmic approach ?
>>>
>>>     --
>>>     ancestor = edited.parent;
>>>     found = false;
>>>     while (found==false and ancestor!=null) {
>>>     if (ancestor == current_user)
>>>     found= true;
>>>     ancestor = ancestor.parent
>>>     }
>>>     return found;
>>>     --
>>>
>>>     I came from C++ background rather than Perl, so I am sorry if it
>>>     looks more like C++ but I hope you got the idea. Not that
>>>     there's a bit of recursive going on there.
>>>
>>>     Sindharta
>>>
>>>     *jakac <jakac at iprom.si>* wrote:
>>>
>>>         Hello!
>>>
>>>         I need a help on building a model for user that has a
>>>         permission to edit
>>>         other users that were created by this user or any of his
>>>         children, grandchildren etc.
>>>         (difficult sentence, I know)
>>>
>>>         To make this a little more understandable here's my database
>>>         table:
>>>
>>>         - user_id
>>>         - username
>>>         - password
>>>         - various other data such as fname, lname, address etc.
>>>         - parent_id
>>>
>>>         Column "parent_id" has a value of "user_id" that created one
>>>         user. There is also
>>>         a "superadministrator" with parent_id '0' that can edit
>>>         everybody.
>>>
>>>         Now I would like to build a model that I can use in my
>>>         controllers like:
>>>
>>>         if ( user_id is child,grandchild,gradgrandchild.... of
>>>         logged in $c->user ) {
>>>         # has permisson to edit
>>>         } else {
>>>         # doesn't have a permission to edit
>>>         }
>>>
>>>         Example:
>>>         - superadmin
>>>         |__- foo
>>>         |__- bar
>>>         |____- john
>>>         |______- doe
>>>
>>>         In this example:
>>>         - superadmin can edit anybody,
>>>         - bar can edit john & doe
>>>         - john can edit only doe
>>>         - foo can't edit anybody since he has no children
>>>         And there can be unlimited levels of users...
>>>
>>>         There is no problem with permission to edit first child
>>>         since I can just compare
>>>         logged in user's ID with edited user's parent_id but when
>>>         edited user is grandchild,
>>>         grandgrandchild, (grand * n) child of $c->user then I need
>>>         some kind of model
>>>         to return true/false value.
>>>
>>>         I've never done that and I am also new to Catalyst so any
>>>         help would be appreciated.
>>>         Thank you!
>>>
>>>
>>>         JakaC. _______________________________________________
>>>         List: Catalyst at lists.scsys.co.uk
>>>         Listinfo:
>>>         http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>>>         Searchable archive:
>>>         http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>>>         Dev site: http://dev.catalyst.perl.org/
>>>
>>>
>>>     ------------------------------------------------------------------------
>>>     Power up the Internet with Yahoo! Toolbar.
>>>     <http://pr.mail.yahoo.co.jp/toolbar/>
>>>     ------------------------------------------------------------------------
>>>
>>>
>>>     _______________________________________________
>>>
>>>     List: Catalyst at lists.scsys.co.uk
>>>
>>>     Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>>>
>>>     Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>>>
>>>     Dev site: http://dev.catalyst.perl.org/
>>>
>>>       
>>     _______________________________________________
>>     List: Catalyst at lists.scsys.co.uk
>>     Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>>     Searchable archive:
>>     http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>>     Dev site: http://dev.catalyst.perl.org/
>>
>>
>> ------------------------------------------------------------------------
>> Power up the Internet with Yahoo! Toolbar.
>> <http://pr.mail.yahoo.co.jp/toolbar/>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> List: Catalyst at lists.scsys.co.uk
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> Dev site: http://dev.catalyst.perl.org/
>>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20080604/04e51a6e/attachment.htm


More information about the Catalyst mailing list