1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: RoleSet.php 841 2013-08-19 22:19:47Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\RoleSet class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * A role set is a utilitity object to determine the if a specific item (page, 40 * list or asset) is readable. The role ids of the roles assigned to that item 41 * are stored in an array-like structure. The role set provides the methods 42 * RoleSet::canRead(\Scrivo\User $user) and 43 * RoleSet::checkReadPermission(\Scrivo\User $user) that can be used to check 44 * if the specific item is readable. 45 */ 46 class RoleSet implements \ArrayAccess { 47 48 /** 49 * An array of role ids. 50 * @var int[] 51 */ 52 private $roleIds = array(); 53 54 /** 55 * Construct a role set object. 56 */ 57 public function __construct() { 58 $this->roleIds = array(); 59 } 60 61 /** 62 * Add a role id to the role set. 63 * 64 * @param int $offset Not applicable. 65 * @param int $value A role id to set in the array. 66 */ 67 public function offsetSet($offset, $value) { 68 $this->roleIds[$value] = $value; 69 } 70 71 /** 72 * Check if a role id is set at the given index position. 73 * 74 * @param int $index The index position for which to check. 75 * 76 * @return boolean True if a role id was set at that given index postition 77 * false if not. 78 */ 79 public function offsetExists($index) { 80 return isset($this->roleIds[$index]); 81 } 82 83 /** 84 * Illegal method, necessary for the implementation of the ArrayAccess 85 * interface. 86 * 87 * @param int $offset Not applicable. 88 */ 89 public function offsetUnset($offset) { 90 throw new \Scrivo\SystemException("Illegal method"); 91 } 92 93 /** 94 * Get the role id at the given index position. 95 * 96 * @param int $offset The index position for which to get the role id. 97 * 98 * @return int The role id at the given index postion, null if the index 99 * postion was invalid. 100 */ 101 public function offsetGet($offset) { 102 return isset($this->roleIds[$offset]) ? $this->roleIds[$offset] : null; 103 } 104 105 /** 106 * Test of the given user has read access according to this role set 107 * object. 108 * 109 * @param \Scrivo\User $user The user for which to test read access. 110 * 111 * @return boolean True if the user has read access, false if not. 112 */ 113 public function canRead(\Scrivo\User $user) { 114 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null)); 115 116 if ($user->status <= \Scrivo\User::STATUS_EDITOR) { 117 return true; 118 } 119 $rls = array(); 120 foreach ($user->roles as $role) { 121 if ($role->type == \Scrivo\Role::PUBLIC_ROLE) { 122 $rls[] = $role->id; 123 } 124 } 125 return count(array_intersect($rls, $this->roleIds)) != 0; 126 } 127 128 /** 129 * Test of the given user has read access according to this role set 130 * object. 131 * 132 * @param \Scrivo\User $user The user for which to test read access. 133 * 134 * @throws \Scrivo\ApplicationException if no access was granted. 135 */ 136 public function checkReadPermission(\Scrivo\User $user) { 137 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null)); 138 139 if (!$this->canRead($user)) { 140 throw new \Scrivo\ApplicationException("Access violation"); 141 } 142 } 143 144 /** 145 * Test of the given user has write access according to this role set 146 * object. 147 * 148 * @param \Scrivo\User $user The user for which to test write access. 149 * 150 * @return boolean True if the user has write access, false if not. 151 */ 152 public function canWrite(\Scrivo\User $user) { 153 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null)); 154 155 if ($user->status < \Scrivo\User::STATUS_ADMIN) { 156 return true; 157 } 158 $rls = array(); 159 foreach ($user->roles as $role) { 160 if ($role->type == \Scrivo\Role::EDITOR_ROLE) { 161 $rls[] = $role->id; 162 } 163 } 164 return count(array_intersect($rls, $this->roleIds)) != 0; 165 } 166 167 } 168 169 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013