Jump to content


Photo

usergroup permissions


  • Please log in to reply
30 replies to this topic

#21 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 12 October 2006 - 05:42 PM

Le sigh, this is what I think of when you say encapsulation:
http://en.wikipedia....ormation_hiding

Please explain to me how this breaks the concept of encapsulation:
<?php
  // NPermissions.php
  // a simple example of a permission system

  // define our permission constants
  define( '_PERM_MOD_THREAD',     0x00000001 );
  define( '_PERM_MOV_THREAD',     0x00000002 );
  define( '_PERM_DEL_THREAD',     0x00000004 );
  define( '_PERM_BAN_USER',       0x00000008 );
  define( '_PERM_UNBAN_USER',     0x00000010 );
  // more constants would follow depending on the module

  // In this example, I'm going to define a class that will not be instantiated.
  // Instead, the class is being used to create a namespace.
  // The assumption for the functions within the class is that the user
  // permissions are loaded from a DB elsewhere in the program.  The first
  // param of the functions is this value, named $user_perms

  class NPermissions{
    // $user_perms - current user permissions
    // RETURN: true if user has permission, false otherwise
    function Can_ModThread($user_perms){
      return $user_perms & _PERM_MOD_THREAD;
    }

    // $user_perms - current user permissions
    // Update user's permissions to allow modding of a thread
    function Set_ModThread($user_perms){
      return $user_perms | _PERM_MOD_THREAD;
    }

    // $user_perms - current user permissions
    // Update user's permissions to not allow thread modding
    function Rem_ModThread($user_perms){
      return $user_perms & ~_PERM_MOD_THREAD;
    }

    // You would continue in this manner for each of your permissions
    // and possibly provide a few more utility functions as the case may
    // be.
  }
?>
<?php
  // AnotherFile.php
  // Meanwhile, in another part of the city...
  require_once('NPermissions.php');

  // Build our forums
  $Posts = GetForumPosts();
  if(is_array($Posts) && count($Posts)){
    foreach($Posts as $Post){
      // .. some code
      if(NPermissions::Can_ModThread($User->GetPerms())){
        $html .= Link_ModThread();
      }
      // .. some more code
    }
  }
?>

I have to say that's pretty neat, simple, and easy to maintain.  6 months later when you do need to change the implementation, you can update the constants, do away with the constants, or whatever you want.  Just as long as you keep the interface the same, nothing else in the program breaks.

Is this not what encapsulation is?  If this fits your definition of encapsulation, I would be curious how the constants are breaking it.  If it's not, I'd love for you to clarify.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#22 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 12 October 2006 - 06:12 PM

guys guys - back on topic please  ;D the topic of encapsulation seems very irrelevent to the objective, as it's not ESSENTIAL to obey these 'rules' for what newb first asked. (although i'm with roopurt anyway a bit actually on this one. the idea of not using global constants or functions as it breaks 'encapsulation' seems slightly contradicted by using php's own built in functions, constants, etc.)

i'm following this topic myself as i'm keen to find out more - so would be good if the initial question got answered without over complication  ;)
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#23 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 October 2006 - 08:23 PM

Le sigh, this is what I think of when you say encapsulation:
http://en.wikipedia....ormation_hiding

Please explain to me how this breaks the concept of encapsulation:

<?php
  // NPermissions.php
  // a simple example of a permission system

  // define our permission constants
  define( '_PERM_MOD_THREAD',     0x00000001 );
  define( '_PERM_MOV_THREAD',     0x00000002 );
  define( '_PERM_DEL_THREAD',     0x00000004 );
  define( '_PERM_BAN_USER',       0x00000008 );
  define( '_PERM_UNBAN_USER',     0x00000010 );
  // more constants would follow depending on the module

  // In this example, I'm going to define a class that will not be instantiated.
  // Instead, the class is being used to create a namespace.
  // The assumption for the functions within the class is that the user
  // permissions are loaded from a DB elsewhere in the program.  The first
  // param of the functions is this value, named $user_perms

  class NPermissions{
    // $user_perms - current user permissions
    // RETURN: true if user has permission, false otherwise
    function Can_ModThread($user_perms){
      return $user_perms & _PERM_MOD_THREAD;
    }

    // $user_perms - current user permissions
    // Update user's permissions to allow modding of a thread
    function Set_ModThread($user_perms){
      return $user_perms | _PERM_MOD_THREAD;
    }

    // $user_perms - current user permissions
    // Update user's permissions to not allow thread modding
    function Rem_ModThread($user_perms){
      return $user_perms & ~_PERM_MOD_THREAD;
    }

    // You would continue in this manner for each of your permissions
    // and possibly provide a few more utility functions as the case may
    // be.
  }
?>
<?php
  // AnotherFile.php
  // Meanwhile, in another part of the city...
  require_once('NPermissions.php');

  // Build our forums
  $Posts = GetForumPosts();
  if(is_array($Posts) && count($Posts)){
    foreach($Posts as $Post){
      // .. some code
      if(NPermissions::Can_ModThread($User->GetPerms())){
        $html .= Link_ModThread();
      }
      // .. some more code
    }
  }
?>

I have to say that's pretty neat, simple, and easy to maintain.  6 months later when you do need to change the implementation, you can update the constants, do away with the constants, or whatever you want.  Just as long as you keep the interface the same, nothing else in the program breaks.

Is this not what encapsulation is?  If this fits your definition of encapsulation, I would be curious how the constants are breaking it.  If it's not, I'd love for you to clarify.

The constants are references to information outside of the 'capsule' that is a module.. thus: encapsualtion broken. Hard to understand?

#24 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 12 October 2006 - 08:39 PM

So you're saying if you replace the constants with literals it makes all the difference?  It would be the same thing.

The reality is every single, separate module needs to define it's own permission constants that can be imported / referenced by a central controller.  If you are designing a modular site with forums and photo galleries, each of those modules is likely to have it's own permissions.  It is impossible for the controller to store those preferences within the same 16, 32, ..., n-bit field as the modules will step on each other.

The reality is that every single module will need to define it's own permission system to be used only within that module.

Every module will also need to provide a mechanism, designed by the controller, of exporting those preferences so that the controller, and not the module, can set preferences up for specific users or groups of users.

Encapsulation is not broken.  The forum module's constants only exist within that module, the photo gallery module's constants only exist within that module, etc.  The only thing the controller ever needs to know is that this particular user logged in right now has a forum permission of 0xA0F2E0D8 and a photo gallery permission of 0x000013AB

When did I ever say that all constants across all modules will all be placed in some central file?  The situation called for a simple explanation of how bitwise operators could be used to pack multiple values into a single field.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#25 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 October 2006 - 09:03 PM

Where did I specify "literals instead of constants"?

Encapsulation is broken. Re-read the link you posted - the first paragraph says it all.

You are bypassing all interfaces to your class, so you can stop mentioning "it's part of the interface"

#26 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 12 October 2006 - 09:07 PM

chaps - please take note of the original question. whilst this is all good, and i'm picking stuff up from the argument that i didnt know before, the arguments of hex vs dec, encapsulation vs constants, etc are not gonna help the OP.

you both know your stuff - that's pretty obvious - but can you possibly stick to the topic and present a solution rather than a "best practice" argument?

cheers

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#27 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 12 October 2006 - 09:25 PM

Don't have to use hex, can stick with decimal..

<?php

if (!($perm ^ 2))
{
    $perm = $perm | 2;
}

?>


2 looks like a literal to me.

redbull, it would appear that the original question has been answered and that the topic has been changed to, "How do we design a system that can handle not only permissions for a forums module, but any other module we decide to add in the future?"
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#28 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 October 2006 - 09:29 PM

@Roopurt & Jenk

I agree with redbull. Cut out the irrelevant arguments, as far as the topic concerned, or I'll knock you heads together and close the thread. If you want to argue the point ad nauseam, do it in the miscellaneous forum.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#29 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 13 October 2006 - 08:31 AM

Don't have to use hex, can stick with decimal..

<?php

if (!($perm ^ 2))
{
    $perm = $perm | 2;
}

?>


2 looks like a literal to me.

redbull, it would appear that the original question has been answered and that the topic has been changed to, "How do we design a system that can handle not only permissions for a forums module, but any other module we decide to add in the future?"

Nice of you to ignore my reply after that ;)

I'm done.

#30 VBAssassin

VBAssassin
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 03 December 2008 - 02:51 PM

Constants break encapsulation.


Not always...

<?php
    
    class messages {
        
        const TYPE_ERROR = 1;
        const TYPE_CONFIRM = 2;
        
        function add_message($message, $type = TYPE_ERROR) {
            
        }
    }
    
    //normal use
    $message = new messages();
    $message->add_message("Well done!", messages::TYPE_CONFIRM);
    $message->add_message("Warning though, don't do this again", messages::TYPE_ERROR);
    
?>

Source: http://www.coderprof...a-name-for-this

Kind regards,
Scott
Coder Profile - For those want to know more about me :-) or YOU...

#31 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 03 December 2008 - 03:14 PM

Kind of old topic to bump to make a point...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users