Changeset 3676
- Timestamp:
- 03/27/07 20:34:52 (2 years ago)
- Files:
-
- plugins/sfGuardDoctrinePlugin/config/doctrine/schema.yml (added)
- plugins/sfGuardDoctrinePlugin/config/doctrine/sfGuardGroup.yml (deleted)
- plugins/sfGuardDoctrinePlugin/config/doctrine/sfGuardGroupPermission.yml (deleted)
- plugins/sfGuardDoctrinePlugin/config/doctrine/sfGuardPermission.yml (deleted)
- plugins/sfGuardDoctrinePlugin/config/doctrine/sfGuardRememberKey.yml (deleted)
- plugins/sfGuardDoctrinePlugin/config/doctrine/sfGuardUser.yml (deleted)
- plugins/sfGuardDoctrinePlugin/config/doctrine/sfGuardUserGroup.yml (deleted)
- plugins/sfGuardDoctrinePlugin/config/doctrine/sfGuardUserPermission.yml (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/generated/BasesfGuardGroup.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/generated/BasesfGuardGroupPermission.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/generated/BasesfGuardPermission.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/generated/BasesfGuardRememberKey.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/generated/BasesfGuardUser.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/generated/BasesfGuardUserGroup.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/generated/BasesfGuardUserPermission.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/sfGuardUser.class.php (modified) (7 diffs)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/sfGuardUserGroup.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/model/doctrine/sfGuardUserPermission.class.php (deleted)
- plugins/sfGuardDoctrinePlugin/lib/user/sfGuardSecurityUser.class.php (modified) (4 diffs)
- plugins/sfGuardDoctrinePlugin/modules/sfGuardGroup/config/generator.yml (modified) (1 diff)
- plugins/sfGuardDoctrinePlugin/modules/sfGuardUser/config/generator.yml (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfGuardDoctrinePlugin/lib/model/doctrine/sfGuardUser.class.php
r3551 r3676 5 5 class sfGuardUser extends BasesfGuardUser 6 6 { 7 const DEFAULT_PROFILE_CLASS = 'sfGuardUserProfile';8 const DEFAULT_PROFILE_FIELD_NAME = 'user_id';9 10 7 protected 11 $profile = null, 12 $userGroups = null, 13 $userPermissions = null, 14 $groupPermissions = null; 8 $allPermissions = null; 15 9 16 10 public function __toString() 17 11 { 18 return $this->get Username();12 return $this->get('username'); 19 13 } 20 14 21 15 public function filterSetPassword( $password ) 22 16 { 17 # FIXME: why is this necessary? 23 18 if ( !$password ) 24 19 { 25 return $this->get Password();20 return $this->get('password'); 26 21 } 27 22 28 $salt = md5( rand( 100000, 999999 ) . $this->get Username() );29 $this->set Salt($salt );23 $salt = md5( rand( 100000, 999999 ) . $this->get('username') ); 24 $this->set('salt', $salt ); 30 25 $algorithm = sfConfig::get( 'app_sf_guard_plugin_algorithm_callable', 'sha1' ); 31 26 $algorithmAsStr = is_array( $algorithm ) ? $algorithm[ 0 ].'::' . $algorithm[ 1 ] : $algorithm; … … 34 29 throw new sfException( sprintf( 'The algorithm callable "%s" is not callable.', $algorithmAsStr ) ); 35 30 } 36 $this->set Algorithm($algorithmAsStr );31 $this->set('algorithm', $algorithmAsStr ); 37 32 38 33 return call_user_func_array( $algorithm, array( $salt . $password ) ); 39 }40 41 public function setPasswordBis( $password )42 {43 return null;44 34 } 45 35 … … 48 38 if ( $callable = sfConfig::get( 'app_sf_guard_plugin_check_password_callable' ) ) 49 39 { 50 return call_user_func_array( $callable, array( $this->get Username(), $password ) );40 return call_user_func_array( $callable, array( $this->get('username'), $password ) ); 51 41 } 52 42 else 53 43 { 54 $algorithm = $this->get Algorithm();44 $algorithm = $this->get('algorithm'); 55 45 if ( false !== $pos = strpos( $algorithm, '::' ) ) 56 46 { … … 62 52 } 63 53 64 return $this->get Password() == call_user_func_array( $algorithm, array( $this->getSalt() . $password ) );54 return $this->get('password') == call_user_func_array( $algorithm, array( $this->get('salt') . $password ) ); 65 55 } 66 }67 68 public function getProfile()69 {70 if ( null === $this->profile )71 {72 $profileClass = sfConfig::get( 'app_sf_guard_plugin_profile_class', self::DEFAULT_PROFILE_CLASS );73 74 if ( !class_exists( $profileClass ) )75 {76 throw new sfException( sprintf( 'The user profile class "%s" does not exist.', $profileClass ) );77 }78 79 $fieldName = sfConfig::get( 'app_sf_guard_plugin_profile_field_name', self::DEFAULT_PROFILE_FIELD_NAME );80 81 $foreignKeyColumnExists = sfDoctrine::getTable( $profileClass )->hasColumn( $fieldName );82 83 if ( !$foreignKeyColumnExists )84 {85 throw new sfException( sprintf( 'The user profile class "%s" does not contain a "%s" column.', $profileClass, $fieldName ) );86 }87 88 $this->profile = sfDoctrine::queryFrom( $profileClass )->where( "{$profileClass}.{$fieldName} = ?", $this->getId() )->execute()->getFirst();89 90 if ( !$this->profile )91 {92 $this->profile = new $profileClass();93 $method = 'set' . sfInflector::camelize( $fieldName );94 $this->profile->{$method}( $this->getId() );95 }96 }97 98 return $this->profile;99 56 } 100 57 … … 106 63 throw new Exception( sprintf( 'The group "%s" does not exist.', $name ) ); 107 64 } 108 109 $ug = new sfGuardUserGroup(); 110 $ug->setUserId( $this->getId() ); 111 $ug->setGroupId( $group->getId() ); 112 113 $ug->save(); 65 66 $this->get('groups')->add($group); 114 67 } 115 68 … … 117 70 { 118 71 $permission = sfDoctrine::getTable('sfGuardGroup')->retrieveByName( $name ); 119 if ( !$permission )72 if ( !$permission->exists() ) 120 73 { 121 74 throw new Exception( sprintf( 'The permission "%s" does not exist.', $name ) ); 122 75 } 123 76 124 $up = new sfGuardUserPermission(); 125 $up->setUserId( $this->getId() ); 126 $up->setPermissionId( $permission->getId() ); 127 128 $up->save(); 77 $this->get('permissions')->add($permission); 129 78 } 130 79 131 80 public function hasGroup( $name ) 132 81 { 133 if ( !$this->userGroups ) 134 { 135 $this->getGroups(); 136 } 137 138 return isset( $this->userGroups[ $name ] ); 139 } 140 141 public function getGroups() 142 { 143 if ( !$this->userGroups ) 144 { 145 $this->userGroups = array(); 146 147 foreach ( $this->getUsersGroups() as $group ) 148 { 149 if ( $group->exists() ) 150 { 151 $this->userGroups[ $group->getName() ] = $group; 152 } 153 } 154 } 155 156 return $this->userGroups; 82 $group = sfDoctrine::queryFrom('sfGuardGroup')->where('sfGuardGroup.name = ? AND sfGuardGroup.users.id = ?', array($name, $this->get('id')))->execute()->getFirst(); 83 return $group->exists(); 157 84 } 158 85 159 86 public function getGroupNames() 160 87 { 161 return array_keys( $this->getGroups() ); 88 # FIXME: won't work since collections are not arrays? 89 return array_keys( $this->get('groups') ); 162 90 } 163 91 164 92 public function hasPermission( $name ) 165 93 { 166 if ( !$this->userPermissions ) 94 $permission = sfDoctrine::queryFrom('sfGuardPermission')->where('sfGuardPermission.name = ? AND sfGuardPermission.users.id = ?', array($name, $this->get('id')))->execute()->getFirst(); 95 return $permission->exists(); 96 } 97 98 99 // merge of permission in a group + permissions 100 public function getAllPermissions() 101 { 102 if ( !$this->allPermissions ) 167 103 { 168 $this->getPermissions(); 104 $this->allPermissions = array(); 105 106 foreach ( $this->get('groups') as $group ) 107 { 108 foreach ( $group->get('permissions') as $permission ) 109 { 110 $this->allPermissions[ $permission->getName() ] = $permission; 111 } 112 113 } 114 115 # FIXME: check that array_merge works with collections... 116 $this->allPermissions = array_merge_recursive( $this->allPermissions, $this->get('permissions') ); 169 117 } 170 118 171 return isset( $this->userPermissions[ $name ] ); 172 } 173 174 public function getPermissions() 175 { 176 if ( !$this->userPermissions ) 177 { 178 $this->userPermissions = array(); 179 180 foreach ( $this->getUsersPermissions() as $permission ) 181 { 182 if ( $permission->exists() ) 183 { 184 $this->userPermissions[ $permission->getName() ] = $permission; 185 } 186 } 187 } 188 189 return $this->userPermissions; 119 return $this->allPermissions; 190 120 } 191 121 192 122 public function getPermissionNames() 193 123 { 194 return array_keys( $this->getPermissions() ); 195 } 196 197 // merge of permission in a group + permissions 198 public function getAllPermissions() 199 { 200 if ( !$this->groupPermissions ) 201 { 202 $this->groupPermissions = array(); 203 204 foreach ( $this->getUsersGroups() as $group ) 205 { 206 if ( $group->exists() ) 207 { 208 foreach ( $group->getGroupsPermissions() as $permission ) 209 { 210 if ( $permission->exists() ) 211 { 212 $this->groupPermissions[ $permission->getName() ] = $permission; 213 } 214 } 215 } 216 } 217 218 $this->groupPermissions = array_merge_recursive( $this->groupPermissions, $this->getPermissions() ); 219 } 220 221 return $this->groupPermissions; 124 $q = new Doctrine_Query(); 125 $names = $q->select('p.name')->from('sfGuardPermission p')->where('p.users.id = ?', $this->get('id'))->execute(); 126 return $names; 222 127 } 223 128 224 129 public function getAllPermissionNames() 225 130 { 131 # FIXME: won't work 226 132 return array_keys( $this->getAllPermissions() ); 227 133 } … … 229 135 public function reloadGroupsAndPermissions() 230 136 { 231 $this->userGroups = null; 232 $this->userPermissions = null; 233 $this->groupPermissions = null; 137 $this->allPermissions = null; 234 138 } 235 139 236 public function delete( Doctrine_Connection $con = null)140 public function set($name, $value, $load = true) 237 141 { 238 // We check for profileClass and fieldName existence here because it is ok if they don't 239 // exist, but if they don't exist and getProfile() is called, an Exception is thrown. 142 // do nothing if trying to set the phony password_bis field 143 if ($name == 'password_bis') 144 return; 240 145 241 $profileClass = sfConfig::get( 'app_sf_guard_plugin_profile_class', self::DEFAULT_PROFILE_CLASS ); 242 $fieldName = sfConfig::get( 'app_sf_guard_plugin_profile_field_name', self::DEFAULT_PROFILE_FIELD_NAME ); 243 244 // First, check to see if class exists 245 if ( class_exists( $profileClass ) ) 246 { 247 // Then see if foreign key column exists. 248 // Foreign key column existence will fail with Doctrine throwing an exception 249 // if the profileClass doesn't actually exist. 250 $foreignKeyColumnExists = sfDoctrine::getTable( $profileClass )->hasColumn( $fieldName ); 251 252 if ( $foreignKeyColumnExists && $profile = $this->getProfile() ) 253 { 254 $profile->delete(); 255 } 256 } 257 258 return parent::delete(); 259 } 260 261 public function set( $name, $value, $load = true ) 262 { 263 $method = 'set' . sfInflector::camelize( $name ); 264 265 if ( !$this->getTable()->hasColumn( $name ) && 266 method_exists( $this, $method ) ) 267 { 268 return $this->{$method}( $value ); 269 } 270 271 return parent::set( $name, $value, $load ); 272 } 273 274 public function get( $name, $invoke = true ) 275 { 276 $method = 'set' . sfInflector::camelize( $name ); 277 278 if ( !$this->getTable()->hasColumn( $name ) && 279 method_exists( $this, $method ) ) 280 { 281 return $this->{$method}(); 282 } 283 284 return parent::get( $name, $invoke ); 146 parent::set($name, $value, $load); 285 147 } 286 148 } plugins/sfGuardDoctrinePlugin/lib/user/sfGuardSecurityUser.class.php
r3544 r3676 43 43 { 44 44 // signin 45 $this->setAttribute( 'user_id', $user->get Id(), 'sfGuardSecurityUser' );45 $this->setAttribute( 'user_id', $user->get('id'), 'sfGuardSecurityUser' ); 46 46 $this->setAuthenticated( true ); 47 47 $this->addCredentials( $user->getAllPermissionNames() ); … … 52 52 // save last login 53 53 $current_time_value = $dateFormat->format( time(), 'I' ); 54 $user->set LastLogin($current_time_value );54 $user->set('last_login', $current_time_value ); 55 55 $user->save(); 56 56 … … 71 71 // save key 72 72 $rk = new sfGuardRememberKey(); 73 $rk->set RememberKey($key );74 $rk->set UserId( $user);75 $rk->set IpAddress($_SERVER[ 'REMOTE_ADDR' ] );73 $rk->set('remember_key', $key ); 74 $rk->set('user', $user); 75 $rk->set('ip_address', $_SERVER[ 'REMOTE_ADDR' ] ); 76 76 $rk->save(); 77 77 … … 122 122 return $this->user; 123 123 } 124 125 // add some proxy method to the sfGuardUser instance126 127 public function __toString()128 {129 return $this->getGuardUser()->__toString();130 }131 132 public function getUsername()133 {134 return $this->getGuardUser()->getUsername();135 }136 137 public function getEmail()138 {139 return $this->getGuardUser()->getEmail();140 }141 142 public function setPassword( $password )143 {144 $this->getGuardUser()->setPassword( $password );145 }146 147 public function checkPassword( $password )148 {149 return $this->getGuardUser()->checkPassword( $password );150 }151 152 public function hasGroup( $name )153 {154 return $this->getGuardUser()->hasGroup( $name );155 }156 157 public function getGroups()158 {159 return $this->getGuardUser()->getUserGroups();160 }161 162 public function getGroupNames()163 {164 return $this->getGuardUser()->getGroupNames();165 }166 167 public function hasPermission( $name )168 {169 return $this->getGuardUser()->hasPermission( $name );170 }171 172 public function getPermissions()173 {174 return $this->getGuardUser()->getPermissions();175 }176 177 public function getPermissionNames()178 {179 return $this->getGuardUser()->getPermissionNames();180 }181 182 public function getAllPermissions()183 {184 return $this->getGuardUser()->getAllPermissions();185 }186 187 public function getAllPermissionNames()188 {189 return $this->getGuardUser()->getAllPermissionNames();190 }191 192 public function getProfile()193 {194 return $this->getGuardUser()->getProfile();195 }196 197 public function addGroupByName( $name )198 {199 return $this->getGuardUser()->addGroupByName( $name );200 }201 202 public function addPermissionByName( $name )203 {204 return $this->getGuardUser()->addPermissionByName( $name );205 }206 124 } plugins/sfGuardDoctrinePlugin/modules/sfGuardGroup/config/generator.yml
r3544 r3676 12 12 title: Edit "%%name%%" group 13 13 fields: 14 groups_permissions: { name: Permisssions, type: doctrine_admin_double_list, params: associated_label=Assigned unassociated_label=Available }15 display: [ name, description, groups_permissions ]14 permissions: { name: Permisssions, type: doctrine_admin_check_list, params: associated_label=Assigned unassociated_label=Available } 15 display: [ name, description, permissions ] plugins/sfGuardDoctrinePlugin/modules/sfGuardUser/config/generator.yml
r3544 r3676 16 16 password: { name: 'Password' } 17 17 password_bis: { name: 'Password (again)' } 18 users_groups: { name: 'Groups/Roles', type: doctrine_admin_double_list, params: associated_label=Assigned unassociated_label=Available }19 users_permissions: { name: 'Permissions', type: doctrine_admin_double_list, params: associated_label=Assigned unassociated_label=Available }18 groups: { name: 'Groups/Roles', type: doctrine_admin_check_list, params: associated_label=Assigned unassociated_label=Available } 19 permissions: { name: 'Permissions', type: doctrine_admin_check_list, params: associated_label=Assigned unassociated_label=Available } 20 20 last_login: { name: 'Last Login', type: plain } 21 21 is_active: { name: 'Active?' } … … 23 23 "NONE": [ username, _password, _password_bis ] 24 24 "Information": [ last_login ] 25 "Permissions and Groups": [ is_active, users_groups, users_permissions ]25 "Permissions and Groups": [ is_active, groups, permissions ]