[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/dao/userpermission.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/dao/daocacheconstants.properties.php" ); 6 7 define( "PERMISSION_BLOG_USER", 2 ); 8 9 /** 10 * Handles user permissions. 11 * 12 * \ingroup DAO 13 */ 14 class UserPermissions extends Model 15 { 16 17 function UserPermissions() 18 { 19 $this->Model(); 20 $this->table = $this->getPrefix()."users_permissions"; 21 } 22 23 /** 24 * Retrieves the permissions for a given user. 25 * 26 * @param userId The user identifier 27 * @param blogId The blog identifier 28 * @return An Array of UserPermission objects with all the different permissions 29 * that the user has for the given blog. the array can be empty if the user has no 30 * privileges over a blog. 31 */ 32 function getUserPermissions( $userId, $blogId ) 33 { 34 $perms = Array(); 35 36 // get all the user permissions from the db table 37 $userPerms = $this->getMany( "user_id", 38 $userId, 39 CACHE_USER_PERMISSIONS ); 40 41 if( !is_array( $userPerms )) 42 $userPerms = Array(); 43 44 foreach( $userPerms as $perm ) { 45 if( $perm->getBlogId() == $blogId ) 46 $perms[$perm->getPermissionId()] = $perm; 47 } 48 49 return( $perms ); 50 } 51 52 /** 53 * Grants a permission to a user, for a given blog. 54 * Any permission can be granted, as long as it exists. 55 * 56 * @param perm A UserPermission object containing information abotu the permissions 57 * and user that we'd like to grant 58 * @param doNotCleanCache Whether we should ignore the code that triggers the cache. You should 59 * probably not use this parameter if in doubt. 60 * @return Returns true if the permission was set or already existed, or false 61 * if there was any problem. 62 */ 63 function grantPermission( &$perm, $doNotCleanCache = false ) 64 { 65 // check if the permission has already been granted 66 $userPerms = $this->getUserPermissions( $perm->getUserId(), $perm->getUserId()); 67 $found = false; 68 foreach( $userPerms as $userPerm ) { 69 if( $userPerm->getUserId() == $perm->getUserId() && $userPerm->getBlogId() == $perm->getBlogId() && 70 $userPerm->getPermissionId() == $perm->getPermissionId()) { 71 $found = true; 72 break; 73 } 74 } 75 if( $found ) 76 return( true ); 77 78 // if not, grant it now 79 if(( $result = $this->add( $perm ))) { 80 if( !$doNotCleanCache ) { 81 $this->_cache->removeData( $perm->getUserId(), CACHE_USER_PERMISSIONS ); 82 $this->_cache->removeData( $perm->getUserId(), CACHE_USERINFO ); 83 $userInfo = $perm->getUserInfo(); 84 $this->_cache->removeData( $userInfo->getUserName(), CACHE_USERIDBYNAME ); 85 } 86 } 87 88 return( $result ); 89 } 90 91 /** 92 * Removes a permission from a user, for a given blog. 93 * 94 * @param userId The user which will be revoked the permission 95 * @param blogId The blog to which this permission will be revoked 96 * @param permissionId The identifier of the permission that will be revoked 97 * @return Returns true if the permission didn't exist or if the permission was 98 * successfully removed, or false otherwise. 99 */ 100 function revokePermission( $userId, $blogId, $permissionId ) 101 { 102 // check if the permission has already been removed or doesn't exist 103 $userPerms = $this->getUserPermissions( $userId, $blogId ); 104 foreach( $userPerms as $perm ) { 105 if( $perm->getUserId() == $userId && $perm->getBlogId() == $blogId && 106 $perm->getPermissionId() == $permissionId ) { 107 $found = true; 108 break; 109 } 110 } 111 if( !$found ) 112 return( true ); 113 114 // build the query to remove the row 115 $query = "DELETE FROM ".$this->getPrefix()."users_permissions WHERE user_id = '".Db::qstr( $userId )."' 116 AND blog_id = '".Db::qstr( $blogId ) ."' AND permission_id = '".Db::qstr( $permissionId )."'"; 117 // and execute it 118 $result = $this->Execute( $query ); 119 if( $result ) { 120 $this->_cache->removeData( $userId, CACHE_USER_PERMISSIONS ); 121 } 122 123 return( $result ); 124 } 125 126 /** 127 * Removes all the user permissions from a given blog 128 * 129 * @param userId 130 * @param blogId 131 * @return True if successful or false otherwise 132 */ 133 function revokePermissions( $userId, $blogId ) 134 { 135 // build the query to remove the row 136 $query = "DELETE FROM ".$this->getPrefix()."users_permissions WHERE user_id = '".Db::qstr( $userId )."' 137 AND blog_id = '".Db::qstr( $blogId )."'"; 138 139 // and execute it 140 $result = $this->Execute( $query ); 141 if( $result ) { 142 $this->_cache->removeData( $userId, CACHE_USER_PERMISSIONS ); 143 } 144 145 return( $result ); 146 } 147 148 /** 149 * removes all the permissions for a given blog, useful when we're removing 150 * the blog from the database. 151 * 152 * @param blogId The blog from which we'd like to remove the permissions. 153 * @return true 154 */ 155 function revokeBlogPermissions( $blogId ) 156 { 157 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 158 159 $blogs = new Blogs(); 160 $blogInfo = $blogs->getBlogInfo( $blogId ); 161 if( !$blogInfo ) 162 return false; 163 $blogUsers = $blogInfo->getUsersInfo(); 164 165 if(( $result = $this->delete( "blog_id", $blogId ))) { 166 foreach( $blogUsers as $user ) { 167 $this->_cache->removeData( $user->getId(), CACHE_USER_PERMISSIONS ); 168 } 169 } 170 171 return $result; 172 } 173 174 /** 175 * revokes all the permissions that a user may have 176 * 177 * @param userId The identifier of the user 178 * @return true 179 */ 180 function revokeUserPermissions( $userId ) 181 { 182 if( $result = $this->delete( "user_id", $userId )) { 183 $this->_cache->removeData( $userId, CACHE_USER_PERMISSIONS ); 184 } 185 186 return( $result ); 187 } 188 189 /** 190 * Returns the number of users who have a certain permission 191 * 192 * @param permId 193 */ 194 function getNumUsersWithPermission( $permId ) 195 { 196 $query = "SELECT COUNT(DISTINCT user_id) AS total FROM ".$this->getPrefix()."users_permissions 197 WHERE permission_id = ".Db::qstr( $permId ); 198 199 // execute it 200 $result = $this->Execute( $query ); 201 202 if( !$result ) 203 return 0; 204 205 // if no error, get the result 206 $row = $result->FetchRow(); 207 $result->Close(); 208 209 $total = $row["total"]; 210 if( $total == "" ) $total = 0; 211 212 return $total; 213 } 214 215 /** 216 * Returns all users who have the given permission, using paging if necessary 217 * 218 * @param permId 219 * @param page 220 * @param itemsPerPage 221 * Return an Array of UserInfo objects or an empty array if no users have this 222 * permission. 223 */ 224 function getUsersWithPermission( $permId, $page = -1, $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) 225 { 226 // tbd 227 } 228 229 /** 230 * @private 231 */ 232 function mapRow( $row ) 233 { 234 $perm = new UserPermission( $row["user_id"], 235 $row["blog_id"], 236 $row["permission_id"], 237 $row["id"] ); 238 239 return $perm; 240 } 241 } 242 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |