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: UserRole.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\UserRole class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * Class that represents a user-role relation. 40 * 41 * The User-Role relationship is a 0 to n relation of Scrivo users and Scrivo 42 * roles. This relation can carry an additional entity: the publisher status. 43 * This status is only relevant for editor roles and determines if an editor 44 * is allowed to publish data in an staging environment. 45 * 46 * Note that both static methods are also exposed through the User::getRoles() 47 * and User::assignRoles($roles) methods. This class should not be considered 48 * part of the public API but as private to the User class. 49 */ 50 class UserRole extends \Scrivo\Role { 51 52 /** 53 * The publisher status. 54 * @var boolean 55 */ 56 private $isPublisher; 57 58 /** 59 * Implementation of the readable properties using the PHP magic 60 * method __get(). 61 * 62 * @param string $name The name of the property to get. 63 * 64 * @return mixed The value of the requested property. 65 */ 66 public function __get($name) { 67 if ($name === "isPublisher") { 68 return $this->isPublisher; 69 } 70 return parent::__get($name); 71 } 72 73 /** 74 * Implementation of the writable properties using the PHP magic 75 * method __set(). 76 * 77 * @param string $name The name of the property to set. 78 * @param mixed $value The value of the property to set. 79 */ 80 public function __set($name, $value) { 81 if ($name === "isPublisher") { 82 $this->setIsPublisher($value); 83 return; 84 } 85 parent::__set($name, $value); 86 } 87 88 /** 89 * Set the publisher status for this user-role relation. 90 * 91 * @param boolean $status The publisher status. 92 */ 93 public function setIsPublisher($status) { 94 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 95 array(\Scrivo\ArgumentCheck::TYPE_BOOLEAN) 96 )); 97 98 $this->isPublisher = $status; 99 } 100 101 /** 102 * Select the user-roles for a given user. 103 * 104 * @param \Scrivo\Context $context A connection to a Scrivo database. 105 * @param \Scrivo\User $user A user to create the user-roles for. 106 * 107 * @return \Scrivo\UserRole[roleId] An array containing the selected 108 * user-roles. 109 */ 110 public static function select(\Scrivo\Context $context, $user) { 111 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array(null, null)); 112 try { 113 if (!$user instanceof \Scrivo\User) { 114 throw new \Scrivo\SystemException("Invalid argument type"); 115 } 116 117 $sth = $context->connection->prepare( 118 "SELECT R.role_id, R.type, R.title, R.description, 119 UR.is_publisher FROM role R, user_role UR 120 WHERE R.instance_id = :instId AND UR.instance_id = :instId 121 AND R.role_id = UR.role_id AND UR.user_id = :userId"); 122 123 $context->connection->bindInstance($sth); 124 $sth->bindValue(":userId", 125 \Scrivo\User::patchId($user->id), \PDO::PARAM_INT); 126 127 $res = array(); 128 $sth->execute(); 129 130 while ($rd = $sth->fetch(\PDO::FETCH_ASSOC)) { 131 132 $li = new UserRole(); 133 $li->setFields($context, $rd); 134 $li->isPublisher = $rd["is_publisher"] == 1; 135 136 $res[$li->id] = $li; 137 } 138 139 return $res; 140 141 } catch(\PDOException $e) { 142 throw new \Scrivo\ResourceException($e); 143 } 144 } 145 146 /** 147 * Set the user roles for a given user. 148 * 149 * The user roles to set is either an array of UserRole or stdClass 150 * objects. stdClass need to contain the members id and isPublisher. 151 * 152 * Note: this sets all the roles for the user at once. So not giving the 153 * the roles effectivily clears the roles for the given user. 154 * 155 * @param \Scrivo\Context $context A connection to a Scrivo database. 156 * @param \Scrivo\User $user A user to set the user-roles for. 157 * @param \Scrivo\UserRole[]|object[] $roles A new set of user-roles for 158 * the given user. 159 */ 160 public static function set( 161 \Scrivo\Context $context, \Scrivo\User $user, array $roles) { 162 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 163 null, null, null)); 164 try { 165 166 $sth = $context->connection->prepare( 167 "DELETE FROM user_role WHERE 168 instance_id = :instId AND user_id = :userId"); 169 170 $context->connection->bindInstance($sth); 171 $sth->bindValue(":userId", $user->id, \PDO::PARAM_INT); 172 173 $sth->execute(); 174 175 if ($roles) { 176 $sth = $context->connection->prepare( 177 "INSERT INTO user_role 178 (instance_id, role_id, user_id, is_publisher) 179 VALUES (:instId, :roleId, :userId, :publisher)"); 180 181 $context->connection->bindInstance($sth); 182 $sth->bindValue(":userId", $user->id, \PDO::PARAM_INT); 183 184 foreach ($roles as $role) { 185 $sth->bindValue(":roleId", $role->id, \PDO::PARAM_INT); 186 $sth->bindValue(":publisher", 187 (bool)$role->isPublisher, \PDO::PARAM_BOOL); 188 189 $sth->execute(); 190 } 191 } 192 193 } catch(\PDOException $e) { 194 throw new \Scrivo\ResourceException($e); 195 } 196 } 197 198 } 199 200 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013