SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class design: users, permissions, and more

    I'm working on a system that involves several objects, and I'm getting a little lost in the woods trying to plan the best structure.

    The bits I'm concerned with are users and permissions.

    First, I have a model class that represents a user. Then I need special representation for the user that is currenctly logged in. With the way my classes are setup now, this would involve a subclass of the user class, because there's specific logic in the user class' constructor that needs to change.

    I surmised I might use a factory to create and return the correct type of user object based on input and other environment criteria.

    So we're looking at something like this

    PHP Code:
    class Model{}
    class 
    User extends Model{}
    class 
    CurrentUser extends User{}
    class 
    UserFactory{}
    class 
    UserPermissions{}

    $user UserFactory::getUser$customerNo$login );
    $perm = new UserPermissions$user ); 
    But I'm not sure I'm crazy about doing permissions this way. AT the same time, I don't feel I wan't to bundle it all into the User class either. I'm trying to maintain separation of reponsibility without creating tons of classes in the process.

    Would I be better off creating the UserPermissions object with composition? Does my design for a user/current-user make sense? etc.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One you could have use composition without the extra inheritence is to simply decorate the user object. That way, you can have a loggedInUserDecorator, invalidUserDecorator, superAdminUserDecorator i suppose? This way you can keep your inheritance tree quite clean and simply pass a permissions object into any users constructor.

    It' just a thought.

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nah. For one, I don't think that's a proper application of the decorator pattern (although I could be mistaken there) Even if it was, how would I know to decorate a user object with, say, the invalid decorator, if I didn't yet know the user object was invalid?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The factory would decide if the user object was invalid, or admin or whatever, so it would work transparently. From the code perspective, it would remain as:
    PHP Code:
    $user userfactory::get(42);
    $user->setPerms(permissionsFactory::getbyUser($user)); 
    And you are unaware of the type of user returned, or the type of permissions assigned to it. You leave it up to other parts of the application to decide what to show / do to an invalidUser or superAdminUser.

  5. #5
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do the Permission class needs to know about user?
    Annotations support for PHP5
    TC/OPT™ Group Leader

  6. #6
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johno View Post
    Why do the Permission class needs to know about user?
    ... to get the permission levels specific to that user ... ?

  7. #7
    SitePoint Zealot johno's Avatar
    Join Date
    Sep 2003
    Location
    Bratislava, Slovakia
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about the User knowing about his Permissions and not te other way around.

    I still don't get the point why do you need to pass User class to Permission constructor.
    Annotations support for PHP5
    TC/OPT™ Group Leader


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •