📄 03-perm.sgml
字号:
<!-- $Id: 03-perm.sgml,v 1.1.1.1 2000/04/17 16:40:02 kk Exp $ --><sect1>Perm<p>Permission management relies on an authenticated session. Itassociates a set of required permissions with a page. The actualpage content is only visible to users with ALL matchingpermissions; all other users are shown a screen of your design.<sect2>Instance variables<p><table><tabular ca="">classname<colsep>Serialization helper: The name of this class.<rowsep>permissions<colsep>A hash of (name, permission bit) pairs.<rowsep></tabular><caption>Accessible instance variables.</caption></table><sect2>Instance methods<p><sect3>Accessible instance methods<p><descrip><tag>check($required)</tag><p>Checks that the currently authenticated user has all the rights that are specified in <tt/required/. If not, <tt/perm_invalid()/ is called. If one or more of the required rights or user rights are invalid (not to be found in the permissions hash), <tt/perm_invalid()/ is called as well.<tag>have_perm($required)</tag><p>Similar to <tt/check()/ in usage, only that it doesn't halt thesession if the user doesn't have the appropriate rights: Thisfunction returns true, if the user has the required rights,false otherwise.<tag>perm_sel($name, $current = "", $class = "")</tag><p>This function returns a <tt/SELECT/-tag with the given<tt/name/. Within this tag, all available permission values from<tt/$perm->permissions/ are contained as <tt/OPTION/ tags.<p>If you supply a value for <tt/current/, the permission valuethat matches <tt/current/ is <tt/SELECTED/. If you supply avalue for <tt/class/, the tags are marked with that CSSstylesheet class.</descrip><sect3>Internal instance methods<p> <descrip> <tag>permsum($rights)</tag><p>Logically or's all the rights and returns a pair <tt/(valid,or_result)/. If valid is true, an <tt/or_result/is provided. If valid is false, the <tt/or_result/ isundefined and one or more of the rights do not exist at all.This is a severe error and the application should be halted atonce.<tag>perm_invalid($does_have, $must_have)</tag><p>Called in case of an access violation. <tt/does_have/ is a stringlisting the rights the user actually has. <tt/must_have/ are therights the page requires.</descrip><sect2>Example<p>Use a subclass of <tt/Perm/ to provide parameters for yourpermission class and to implement your own <tt/perm_invalid/function.<tscreen><code>class My_Perm extends Perm { var $classname = "My_Perm"; var $permissions = array ( "user" => 1, "author" => 2, "editor" => 4, "moderator" => 8, "admin" => 16 ); function perm_invalid($does_have, $must_have) { global $perm, $auth, $sess; include("perminvalid.ihtml"); }}</code></tscreen>Use the page management functions (see above) to use yourpermission subclass. The feature name for permissionmanagement is <tt/perm/; provide the name of your <tt/Perm/ subclass asa parameter to the <tt/perm/ feature. The <tt/perm/ feature requires the<tt/sess/ feature and the <tt/auth/ feature:<tscreen><code> page_open(array("sess" => "My_Session", "auth" => "My_Auth", "perm" => "My_Perm"));</code></tscreen>Use the <tt/check()/ instance method to protect your page:<tscreen><code> $perm->check("admin"); ## This page is for users with admin rights only.</code></tscreen>Use <tt/have_perm()/ to create protected functionality on apage:<tscreen><code><?php if ($perm->have_perm("admin")): ?> <h1>Admin only functionality</h1><?php endif; ?></code></tscreen><sect2>How permissions work <p> Your subclass of <tt/Perm/ defines an array <tt/$permissions/,which translates permission names into bit patterns. Forexample, the definition of <tt/Example_Perm/ in the distributed<tt/local.inc/ defines the names <tt/user/, <tt/author/,<tt/editor/, <tt/supervisor/ and <tt/admin/, all of whichtranslate into a bit pattern with a single bit set.A user may be assigned any number of permissions as a commaseparated list of permission names (no spaces!) in the<tt/perms/ column of the <tt/auth_user/ table. The effectivepermissions of the user are determined by logically OR'ing thebit patterns of these permissions.A page may require any permissions as a comma separated list ofpermission names (again no spaces!) with the<tt/$perm->check()/ function. The required permissions areagain determined by logically OR'ing the bit patterns of thesepermissions. Similarly, a page function may be protected byrequiring permissions with <tt/$perm->check()/.Access is granted to a protected page or a protected pagefunction, if the effective permissions of the authenticated userhave all the required bits set, that is: If the effectivepermissions of the user logically AND'ed with the requiredpermissions are equal to the required permissions.With the permission names as defined in <tt/Example_Perm/ from thedistribution, a user <tt/kris/ may be defined with <tt/admin/permission in the <tt/auth_user/ table. A page that requires<tt/admin,user/ permission with<tt/$perm->check("user,admin")/ is inaccessible to this user.This is how it is calculated:<tscreen><code>Effective Permissions of User: admin translates into: 16Required Permissions of Page : user,admin translates into: 1 OR 16 == 17Permission Check: Effective Permissions 16 AND Required Permissions 17ARE 16 & 17 = 16MUST BE Required Permissions 17 -> access denied</code></tscreen>The example permissions as defined in <tt/Example_Perm/ from thedistribution are called <em/atomic/ permissions, because each ofthem has only a single bit set. Atomic permissions are thesimplest of all schemes, because they allow for easy permissionchecks: To access a page protected with <tt/user,admin/, youneed to have at least <tt/user,admin/ rights in your<tt/auth_user/ table.Another common scheme used in permission definitions are<tt/inclusive permissions/. In this scheme, each permissiondefinition has all bits of its predecessor set plus one additionbit. For example<tscreen><code>class Inclusive_Perm extends Perm { var $classname = "Inclusive_Perm"; var $permissions = array( "user" => 1, "author" => 3, "editor" => 7, "supervisor" => 15, "admin" => 31 );}</code></tscreen>defines a set of inclusive permissions. In this example, a user<tt/kris/ with <tt/admin/ permissions can easily access a pageprotected with <tt/editor/ permissions. This is how it iscalculated:<tscreen><code>Effective Permissions of User: admin translates into: 31Required Permissions of Page : editor translates into: 7Permission Check: Effective Permissions 31AND Required Permissions 7ARE 31 & 7 = 7MUST BE Required Permissions 7 -> access granted</code></tscreen>Inclusive Permissions are easy to deal with, too, because a userwith a <em/higher/ access level may access all pages or pagefunctions with a <em/lower/ access level.Due to limitations of your machines integer size you can onlydefine up to 31 permission levels.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -