Development

Changeset 3676

You must first sign up to be able to contribute.

Changeset 3676

Show
Ignore:
Timestamp:
03/27/07 20:34:52 (2 years ago)
Author:
chtito
Message:

"doctrinification" of the sfGuardDoctrine plugin; many unnecessary propel overhead removed; proxy methods removed; take advantage of the doctrine many2many native support and of the cross-schema relationships;

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfGuardDoctrinePlugin/lib/model/doctrine/sfGuardUser.class.php

    r3551 r3676  
    55class sfGuardUser extends BasesfGuardUser 
    66{ 
    7     const DEFAULT_PROFILE_CLASS      = 'sfGuardUserProfile'; 
    8     const DEFAULT_PROFILE_FIELD_NAME = 'user_id'; 
    9  
    107    protected 
    11         $profile          = null, 
    12         $userGroups       = null, 
    13         $userPermissions  = null, 
    14         $groupPermissions = null; 
     8        $allPermissions  = null; 
    159 
    1610    public function __toString() 
    1711    { 
    18         return $this->getUsername(); 
     12        return $this->get('username'); 
    1913    } 
    2014 
    2115    public function filterSetPassword( $password ) 
    2216    { 
     17        # FIXME: why is this necessary? 
    2318        if ( !$password ) 
    2419        { 
    25             return $this->getPassword(); 
     20            return $this->get('password'); 
    2621        } 
    2722 
    28         $salt = md5( rand( 100000, 999999 ) . $this->getUsername() ); 
    29         $this->setSalt( $salt ); 
     23        $salt = md5( rand( 100000, 999999 ) . $this->get('username') ); 
     24        $this->set('salt', $salt ); 
    3025        $algorithm = sfConfig::get( 'app_sf_guard_plugin_algorithm_callable', 'sha1' ); 
    3126        $algorithmAsStr = is_array( $algorithm ) ? $algorithm[ 0 ].'::' . $algorithm[ 1 ] : $algorithm; 
     
    3429            throw new sfException( sprintf( 'The algorithm callable "%s" is not callable.', $algorithmAsStr ) ); 
    3530        } 
    36         $this->setAlgorithm( $algorithmAsStr ); 
     31        $this->set('algorithm', $algorithmAsStr ); 
    3732 
    3833        return call_user_func_array( $algorithm, array( $salt . $password ) ); 
    39     } 
    40  
    41     public function setPasswordBis( $password ) 
    42     { 
    43         return null; 
    4434    } 
    4535 
     
    4838        if ( $callable = sfConfig::get( 'app_sf_guard_plugin_check_password_callable' ) ) 
    4939        { 
    50             return call_user_func_array( $callable, array( $this->getUsername(), $password ) ); 
     40            return call_user_func_array( $callable, array( $this->get('username'), $password ) ); 
    5141        } 
    5242        else 
    5343        { 
    54             $algorithm = $this->getAlgorithm(); 
     44            $algorithm = $this->get('algorithm'); 
    5545            if ( false !== $pos = strpos( $algorithm, '::' ) ) 
    5646            { 
     
    6252            } 
    6353 
    64             return $this->getPassword() == call_user_func_array( $algorithm, array( $this->getSalt() . $password ) ); 
     54            return $this->get('password') == call_user_func_array( $algorithm, array( $this->get('salt') . $password ) ); 
    6555        } 
    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; 
    9956    } 
    10057 
     
    10663            throw new Exception( sprintf( 'The group "%s" does not exist.', $name ) ); 
    10764        } 
    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); 
    11467    } 
    11568 
     
    11770    { 
    11871        $permission = sfDoctrine::getTable('sfGuardGroup')->retrieveByName( $name ); 
    119         if ( !$permission
     72        if ( !$permission->exists()
    12073        { 
    12174            throw new Exception( sprintf( 'The permission "%s" does not exist.', $name ) ); 
    12275        } 
    12376 
    124         $up = new sfGuardUserPermission(); 
    125         $up->setUserId( $this->getId() ); 
    126         $up->setPermissionId( $permission->getId() ); 
    127  
    128         $up->save(); 
     77        $this->get('permissions')->add($permission); 
    12978    } 
    13079 
    13180    public function hasGroup( $name ) 
    13281    { 
    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(); 
    15784    } 
    15885 
    15986    public function getGroupNames() 
    16087    { 
    161         return array_keys( $this->getGroups() ); 
     88        # FIXME: won't work since collections are not arrays? 
     89        return array_keys( $this->get('groups') ); 
    16290    } 
    16391 
    16492    public function hasPermission( $name ) 
    16593    { 
    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 ) 
    167103        { 
    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') ); 
    169117        } 
    170118 
    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; 
    190120    } 
    191121 
    192122    public function getPermissionNames() 
    193123    { 
    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; 
    222127    } 
    223128 
    224129    public function getAllPermissionNames() 
    225130    { 
     131        # FIXME: won't work 
    226132        return array_keys( $this->getAllPermissions() ); 
    227133    } 
     
    229135    public function reloadGroupsAndPermissions() 
    230136    { 
    231         $this->userGroups       = null; 
    232         $this->userPermissions  = null; 
    233         $this->groupPermissions = null; 
     137        $this->allPermissions = null; 
    234138    } 
    235139 
    236     public function delete( Doctrine_Connection $con = null
     140    public function set($name, $value, $load = true
    237141    { 
    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; 
    240145 
    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); 
    285147    } 
    286148} 
  • plugins/sfGuardDoctrinePlugin/lib/user/sfGuardSecurityUser.class.php

    r3544 r3676  
    4343    { 
    4444        // signin 
    45         $this->setAttribute( 'user_id', $user->getId(), 'sfGuardSecurityUser' ); 
     45        $this->setAttribute( 'user_id', $user->get('id'), 'sfGuardSecurityUser' ); 
    4646        $this->setAuthenticated( true ); 
    4747        $this->addCredentials( $user->getAllPermissionNames() ); 
     
    5252        // save last login 
    5353        $current_time_value = $dateFormat->format( time(), 'I' ); 
    54         $user->setLastLogin( $current_time_value ); 
     54        $user->set('last_login', $current_time_value ); 
    5555        $user->save(); 
    5656 
     
    7171            // save key 
    7272            $rk = new sfGuardRememberKey(); 
    73             $rk->setRememberKey( $key ); 
    74             $rk->setUserId( $user ); 
    75             $rk->setIpAddress( $_SERVER[ 'REMOTE_ADDR' ] ); 
     73            $rk->set('remember_key', $key ); 
     74            $rk->set('user', $user); 
     75            $rk->set('ip_address', $_SERVER[ 'REMOTE_ADDR' ] ); 
    7676            $rk->save(); 
    7777 
     
    122122        return $this->user; 
    123123    } 
    124  
    125     // add some proxy method to the sfGuardUser instance 
    126  
    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     } 
    206124} 
  • plugins/sfGuardDoctrinePlugin/modules/sfGuardGroup/config/generator.yml

    r3544 r3676  
    1212      title:          Edit "%%name%%" group 
    1313      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  
    1616        password:                 { name: 'Password' } 
    1717        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 } 
    2020        last_login:               { name: 'Last Login', type: plain } 
    2121        is_active:                { name: 'Active?' } 
     
    2323        "NONE":                   [ username, _password, _password_bis ] 
    2424        "Information":            [ last_login ] 
    25         "Permissions and Groups": [ is_active, users_groups, users_permissions ] 
     25        "Permissions and Groups": [ is_active, groups, permissions ]