Re: [Catalyst] Users hierarchy

2008-06-06 Thread Andrew Rodland
On Friday 06 June 2008 10:46:53 pm Jonathan Rockway wrote:
> * On Fri, Jun 06 2008, [EMAIL PROTECTED] wrote:
> > Gulp, that should have been 0 :)
>
> Actually, it should have been "!" as in "if(!$found ...)".  Perl has a
> variety of "false" values including 0, undef, and the empty string.
> Perl's built-in boolean operators return "undef" or "1" as false or
> true, but other libraries use other values ("" and "hey, it worked!",
> for example).

Actually (0==1) is "" and defined, while we're picking nits. The point stands 
that if you want to test for truth you should simply test for truth.

Andrew

___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-06 Thread Jonathan Rockway
* On Fri, Jun 06 2008, [EMAIL PROTECTED] wrote:
> Gulp, that should have been 0 :)

Actually, it should have been "!" as in "if(!$found ...)".  Perl has a
variety of "false" values including 0, undef, and the empty string.
Perl's built-in boolean operators return "undef" or "1" as false or
true, but other libraries use other values ("" and "hey, it worked!",
for example).

Additionally, 0 is "==" to a variety of true values, including the
string "0 but true".

So just use "!$boolean" to test if something is false :)

Regards,
Jonathan Rockway

-- 
print just => another => perl => hacker => if $,=$"

___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-06 Thread sindharta_tanuwijaya
Gulp, that should have been 0 :)

Sindharta

Jonathan Rockway <[EMAIL PROTECTED]> wrote: * On Tue, Jun 03 2008, [EMAIL 
PROTECTED] wrote:
> while ($found==false && $ancestor) {

Perl does not have a "false" operator.

Regards,
Jonathan Rockway

-- 
print just => another => perl => hacker => if $,=$"

___
List: Catalyst@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/

 

 
-
GANBARE! NIPPON! Chance to win 50,000 Yahoo! Points!
___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-04 Thread Jonathan Rockway
* On Tue, Jun 03 2008, [EMAIL PROTECTED] wrote:
> while ($found==false && $ancestor) {

Perl does not have a "false" operator.

Regards,
Jonathan Rockway

-- 
print just => another => perl => hacker => if $,=$"

___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-04 Thread Jonathan Rockway
* On Wed, Jun 04 2008, jakac wrote:
> I checked out DBIx::OO::Tree also but using this module would mean
> that I would have to change my table schema (add columns parent,lft,rgt,mvg)..
> I don't see why this is necessary since the whole thing needs only two colums 
> -
> user_id and parent_id ...? If I know a user_id then I can always get all users
> where their parent_id = this_user_id etc.
>
> Like I said in my previous postings - I really don't want to change my 
> database
> schema because a lot of other applications depend on it. There is no problem
> adding columns lft,rgt and mvg but if I change my primary key from
> "user_id" to "id" and change my column "parent_id" to "parent" like this 
> module
> requires it would mean changing a lot of other older applications which depend
> on this database...
>

What about DBIx::Class::Tree?  I've used this for a user hierarchy,
actually.

Regards,
Jonathan Rockway

-- 
print just => another => perl => hacker => if $,=$"

___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-04 Thread jakac


I checked out DBIx::OO::Tree also but using this module would mean
that I would have to change my table schema (add columns 
parent,lft,rgt,mvg)..
I don't see why this is necessary since the whole thing needs only two 
colums -
user_id and parent_id ...? If I know a user_id then I can always get all 
users

where their parent_id = this_user_id etc.

Like I said in my previous postings - I really don't want to change my 
database

schema because a lot of other applications depend on it. There is no problem
adding columns lft,rgt and mvg but if I change my primary key from
"user_id" to "id" and change my column "parent_id" to "parent" like this 
module
requires it would mean changing a lot of other older applications which 
depend

on this database...

vb wrote:

perhaps DBIx::OO::Tree could help you?

On Wed, Jun 4, 2008 at 1:53 PM, jakac <[EMAIL PROTECTED] 
> wrote:


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!

[EMAIL PROTECTED]
 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 <[EMAIL PROTECTED]> * 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..

[EMAIL PROTECTED]
 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 <[EMAIL PROTECTED]> * 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
   

Re: [Catalyst] Users hierarchy

2008-06-04 Thread vb
perhaps DBIx::OO::Tree could help you?

On Wed, Jun 4, 2008 at 1:53 PM, jakac <[EMAIL PROTECTED]> wrote:

>  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!
>
> [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>* 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..
>
> [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]> <[EMAIL PROTECTED]>* 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@lists.scsys.co.uk
> Listinfo: http://list

Re: [Catalyst] Users hierarchy

2008-06-04 Thread jakac
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!
>
> [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>* 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..
>>
>> [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>* 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 ed

Re: [Catalyst] Users hierarchy

2008-06-04 Thread jakac
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!

[EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>* 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..
>
> [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>* 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@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.
>> 
>> 
>>
>>
>> ___
>>
>> List: Catalyst@lists.scsys.co.uk
>>
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>>
>> Sear

Re: [Catalyst] Users hierarchy

2008-06-03 Thread sindharta_tanuwijaya
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 <[EMAIL PROTECTED]> 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.. 
 
 [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]> 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@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.
   

-

___
List: Catalyst@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@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.
___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-03 Thread jakac

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..

[EMAIL PROTECTED] 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 <[EMAIL PROTECTED]>* 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@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.
> 
> 
>
> ___
> List: Catalyst@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@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/


Re: [Catalyst] Users hierarchy

2008-06-03 Thread John Tate



Check out Nested Trees,  Joe Celko has some articles about them.  Mojomojo
has a pretty complete implementation of one in dbix:class if I remember.
  
Along the same lines, you could look at "modified pre-order tree 
traversal". There's a nice sitepoint article about the method:


http://www.sitepoint.com/article/hierarchical-data-database

We use this for walking up and down large hierarchies and pulling out 
sub-trees. Building the table is a bit of work initially and modifying 
it (which we don't do) will probably need some careful planning, but 
accessing the hierarchy like this is quick and easy.


John.



--
The Wellcome Trust Sanger Institute is operated by Genome Research 
Limited, a charity registered in England with number 1021457 and a 
company registered in England with number 2742969, whose registered 
office is 215 Euston Road, London, NW1 2BE. 


___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-02 Thread sindharta_tanuwijaya
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 <[EMAIL PROTECTED]> 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@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.
___
List: Catalyst@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/


Re: [Catalyst] Users hierarchy

2008-06-02 Thread Wade . Stuart


jakac <[EMAIL PROTECTED]> wrote on 06/02/2008 09:59:37 AM:

> 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!
>


Check out Nested Trees,  Joe Celko has some articles about them.  Mojomojo
has a pretty complete implementation of one in dbix:class if I remember.
http://www.dbmsmag.com/9603d06.html
http://www.intelligententerprise.com/001020/celko.jhtml






>
> JakaC. ___
> List: Catalyst@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@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/


Re: [Catalyst] Users hierarchy

2008-06-02 Thread Jonathan Hall
I make no claim at being an expert, but here's what we do... If there's 
a better way, I'm all ears!


We use two tables.  A Users table, and a Parents table that acts as a 
map between users and all of their possible parents.



CREATE TABLE users (
  user_id  SERIAL PRIMARY KEY,
  parent_id   INT REFERENCES users(user_id)
  ...
);

CREATE TABLE parents (
  user_id  INT REFERENCES users(user_id) ON DELETE CASCADE,
  parent_id  INT REFERNECES users(user_id) ON DELETE CASCADE,
  level  INT,
  PRIMARY KEY (user_id,parent_id)
);


Somewhere you need logic that populates the "parents" table any time a 
user is added/deleted, or re-parented (if supported by your 
application).  The "parents" table should have one entry for every 
parent/child relationship in existence, including each users's 
relationship with themself.  Yes, this means lots of duplicate data.


Suppose your users tree contains 5 users:

User1
+-- User2
+-- User3
   + User4
   + User5

Your "users" table will look like this:

user_id | parent_id
-+---
  1 |
  2 | 1
  3 | 1
  4 | 3
  5 | 3

And your "parents" table should look like this:

user_id | parent_id | level
-+---+---
  1 | 1 | 0
  2 | 1 | 1
  2 | 2 | 0
  3 | 1 | 1
  3 | 3 | 0
  4 | 1 | 2
  4 | 3 | 1
  4 | 4 | 0
  5 | 1 | 2
  5 | 3 | 1
  5 | 5 | 0


The "level" column represents how far away you are from the "top" level 
user.  If your needs are simple enough, this column may not be strictly 
necessary.  If your needs are complex enough, you may also need an 
opposite level column--a "from_top" and "from_bottom" may be more 
appropriate.


We keep this table up-to-date with a plperl stored procedure:


CREATE FUNCTION update_parents_table() RETURNS TRIGGER AS $$
my @ids = ($_TD->{new}{user_id});
while ( @ids ) {
 my $id = shift @ids;
 my $sth = spi_query("SELECT user_id FROM users WHERE parent_id=$id");
 while ( my $row = spi_fetchrow($sth) ) {
   push @ids, $row->{user_id};
 }
 my $parent = $id;
 my $level = 1;
 spi_exec_query("DELETE FROM parents WHERE user_id=$id") if 
$_TD->{event} eq 'UPDATE';
 spi_exec_query("INSERT INTO parents (user_id,parent_id,level) VALUES 
($id,$id,0)");

 while ( $parent ) {
   my $rv = spi_exec_query("SELECT parent_id FROM users WHERE 
user_id=$parent");

   $parent = $rv->{rows}[0]->{parent};
   spi_exec_query("INSERT INTO parents (user_id,parent_id,level) VALUES 
($id,$parent,$level)");

   $level++;
 }
}
return;
$$ LANGUAGE plperl;

CREATE TRIGGER update_parents_table
   AFTER INSERT OR UPDATE ON users
   FOR EACH ROW EXECUTE PROCEDURE update_uid_cache();


The same thing should be easily accomplished in any other stored 
procedure language, or even in DBIC or your application.


Then, to do the query (the part you're actually trying to do!) it's just 
a matter of a join.  To select all of the users that are at user 3 or 
lower in the tree (this will include user 3, 4, and 5 from my example):


SELECT * FROM users JOIN parents ON (users.user_id = parents.user_id) 
WHERE parents.parent_id = 3;

user_id | parent_id | user_id | parent_id | level
-+---+-+---+---
  4 | 3 |   4 | 3 | 1
  5 | 3 |   5 | 3 | 1
  3 | 1 |   3 | 3 | 0


Or for your requirement... ( if ( user_id is 
child,grandchild,gradgrandchild of logged in $c->user ) );


SELECT 1 FROM users JOIN parents ON (users.user_id = parents.user_id) 
WHERE parents.parent_id = ? AND users.user_id = ?;

$c->user->user_id, $target->user_id.

In DBIC (Someone can probably think of better relationship names):

Users.pm:
__PACKAGE__->has_many("parents","Parents", { parent_id => "user_id" });

Parents.pm:
__PACKAGE__->belongs_to("user_parent","Users", { user_id => "parent_id" });


Then in Catalyst:

if ( $c->model->search(
   {  'users.user_id' => $target_user_id,
   'parents.parent_id' => $c->user->user_id },
   { join => "parents" })->first ) {
   # Has permission
} else {
   # Does not have permission
}

That's it... in a nutshell.  Perhaps a bit daunting if it's your first 
go at something like this, but once it makes sense in your head, it 
should be pretty straight-forward to implement and use.


--
Jonathan


Gabriel Vieira wrote:

I usually use a subid to identify child's parent as you do. So I run
bottom to top to get to the major user. With this I can see if the
some ID have access to other.

This may be slow. Let's see what gurus have to say. :)

2008/6/2 jakac <[EMAIL PROTECTED]>:
  

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 

Re: [Catalyst] Users hierarchy

2008-06-02 Thread Gabriel Vieira
I usually use a subid to identify child's parent as you do. So I run
bottom to top to get to the major user. With this I can see if the
some ID have access to other.

This may be slow. Let's see what gurus have to say. :)

2008/6/2 jakac <[EMAIL PROTECTED]>:
> 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@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/
>
>



-- 
Gabriel Vieira

___
List: Catalyst@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/


[Catalyst] Users hierarchy

2008-06-02 Thread jakac

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@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/