]> git.andy128k.dev Git - ipf.git/commitdiff
move auth app code to namespace
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 14 Sep 2014 08:45:03 +0000 (11:45 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 14 Sep 2014 08:45:03 +0000 (11:45 +0300)
ipf/auth/admin.php
ipf/auth/app.php
ipf/auth/commands/createsuperuser.php
ipf/auth/middleware.php
ipf/auth/models.php

index e45bb744e86b71430c004e470e2a29b2f6131e8e..69ff322b2bd876b972a91eb395575d02f868b890 100644 (file)
@@ -1,5 +1,9 @@
 <?php
 
+namespace IPF\Auth\Admin;
+
+use \IPF\Auth\Role as Role;
+use \IPF\Auth\Permission as Permission;
 use \PFF\HtmlBuilder\Text as Text;
 
 function permissionChoices()
@@ -7,7 +11,7 @@ function permissionChoices()
     $choices = array();
     foreach (Permission::query()->fetchAll() as $p) {
         list($appSlug, $componentSlug, $permission) = explode('|', $p->name);
-        $component = IPF_Admin_App::getComponentBySlugs($appSlug, $componentSlug);
+        $component = \IPF_Admin_App::getComponentBySlugs($appSlug, $componentSlug);
 
         if ($component)
             $title = $component->app->getTitle().' | '.$component->verbose_name().' | '.ucfirst($permission);
@@ -20,7 +24,7 @@ function permissionChoices()
     return $choices;
 }
 
-class IPFAuthAdminUserForm extends IPF_ObjectForm
+class UserForm extends \IPF_ObjectForm
 {
     private $isAdd;
 
@@ -28,21 +32,21 @@ class IPFAuthAdminUserForm extends IPF_ObjectForm
     {
         $this->isAdd = $extra['is_add'];
 
-        $this->fields['username'] = new IPF_Form_Field_Varchar(array(
+        $this->fields['username'] = new \IPF_Form_Field_Varchar(array(
             'required'    => true,
             'max_length'  => 32,
             'label'       => __('Username'),
             'help_text'   => __('Required. 32 characters or less. Alphanumeric characters only (letters, digits and underscores).'),
         ));
 
-        $this->fields['password1'] = new IPF_Form_Field_Varchar(array(
+        $this->fields['password1'] = new \IPF_Form_Field_Varchar(array(
             'label'       => __('Password'),
             'required'    => $this->isAdd,
             'max_length'  => 32,
             'widget'      => 'IPF_Form_Widget_PasswordInput'
         ));
 
-        $this->fields['password2'] = new IPF_Form_Field_Varchar(array(
+        $this->fields['password2'] = new \IPF_Form_Field_Varchar(array(
             'label'       => __('Password (again)'),
             'required'    => $this->isAdd,
             'max_length'  => 32,
@@ -50,40 +54,40 @@ class IPFAuthAdminUserForm extends IPF_ObjectForm
             'help_text'   => __('Enter the same password as above, for verification.'),
         ));
 
-        $this->fields['email'] = new IPF_Form_Field_Email(array(
+        $this->fields['email'] = new \IPF_Form_Field_Email(array(
             'required'    => true,
             'max_length'  => 200,
             'label'       => __('E-mail'),
         ));
 
-        $this->fields['is_active'] = new IPF_Form_Field_Boolean(array(
+        $this->fields['is_active'] = new \IPF_Form_Field_Boolean(array(
             'label'       => __('Active'),
             'help_text'   => __('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'),
         ));
 
-        $this->fields['is_staff'] = new IPF_Form_Field_Boolean(array(
+        $this->fields['is_staff'] = new \IPF_Form_Field_Boolean(array(
             'label'       => __('Staff status'),
             'help_text'   => __('Designates whether the user can log into this admin site.'),
         ));
 
-        $this->fields['is_superuser'] = new IPF_Form_Field_Boolean(array(
+        $this->fields['is_superuser'] = new \IPF_Form_Field_Boolean(array(
             'label'       => __('Superuser status'),
             'help_text'   => __('Designates that this user has all permissions without explicitly assigning them.'),
         ));
 
         $permissions = array('is_active', 'is_staff', 'is_superuser');
-        if (IPF_Auth_App::ArePermissionsEnabled()) {
+        if (\IPF_Auth_App::ArePermissionsEnabled()) {
             $permissions[] = 'permissions';
             $permissions[] = 'roles';
 
-            $this->fields['permissions'] = new IPF_Form_Field_MultipleChoice(array(
+            $this->fields['permissions'] = new \IPF_Form_Field_MultipleChoice(array(
                 'label'     => __('Permissions'),
                 'choices'   => permissionChoices(),
                 'widget'    => 'IPF_Form_Widget_SelectMultipleInputCheckbox',
                 'widget_attrs' => array('class' => 'checkgroup'),
             ));
 
-            $this->fields['roles'] = new IPF_Form_Field_MultipleChoice(array(
+            $this->fields['roles'] = new \IPF_Form_Field_MultipleChoice(array(
                 'label'     => __('Groups'),
                 'help_text' => __('In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in.'),
                 'choices'   => Role::query()->fetchPairs('name', 'id'),
@@ -111,7 +115,7 @@ class IPFAuthAdminUserForm extends IPF_ObjectForm
     }
 }
 
-class AdminUser extends IPF_Admin_Component
+class AdminUser extends \IPF_Admin_Component
 {
     private $auth_app;
 
@@ -144,7 +148,7 @@ class AdminUser extends IPF_Admin_Component
             'is_staff',
             'is_superuser',
         );
-        if (IPF_Auth_App::ArePermissionsEnabled()) {
+        if (\IPF_Auth_App::ArePermissionsEnabled()) {
             $columns[] = 'groups';
         }
         return $columns;
@@ -192,7 +196,7 @@ class AdminUser extends IPF_Admin_Component
         } else {
             $extra['is_add'] = true;
         }
-        return new IPFAuthAdminUserForm($data, $user, $extra);
+        return new UserForm($data, $user, $extra);
     }
 
     public function saveObject($form, $user)
@@ -227,25 +231,26 @@ class AdminUser extends IPF_Admin_Component
     protected function objectTools($user)
     {
         return array(
-            'impersonate' => IPF_HTTP_URL::urlForView('IPF_Admin_Views_Impersonate', array($user->id)),
+            'impersonate' => \IPF_HTTP_URL::urlForView('IPF_Admin_Views_Impersonate', array($user->id)),
         );
     }
 
     public function verbose_name() { return __('User'); }
+    public function slug() { return 'user'; }
 }
 
-class IPFAdminRoleForm extends IPF_ObjectForm
+class RoleForm extends \IPF_ObjectForm
 {
     function initFields($extra=array())
     {
-        $this->fields['name'] = new IPF_Form_Field_Varchar(array(
+        $this->fields['name'] = new \IPF_Form_Field_Varchar(array(
             'required' => true,
             'label' => __('Name'),
             'max_length' => 255,
         ));
 
-        if (IPF_Auth_App::ArePermissionsEnabled()) {
-            $this->fields['permissions'] = new IPF_Form_Field_MultipleChoice(array(
+        if (\IPF_Auth_App::ArePermissionsEnabled()) {
+            $this->fields['permissions'] = new \IPF_Form_Field_MultipleChoice(array(
                 'label'     => __('Permissions'),
                 'choices'   => permissionChoices(),
                 'widget'    => 'IPF_Form_Widget_SelectMultipleInputCheckbox',
@@ -255,7 +260,7 @@ class IPFAdminRoleForm extends IPF_ObjectForm
     }
 }
 
-class AdminRole extends IPF_Admin_Component
+class AdminRole extends \IPF_Admin_Component
 {
     public function getItems($searchValue, $filters, $page, $pageSize)
     {
@@ -299,11 +304,11 @@ class AdminRole extends IPF_Admin_Component
     protected function _getForm($role, $data)
     {
         if ($role)
-            return new IPFAdminRoleForm($data, $role, array('initial' => array(
+            return new RoleForm($data, $role, array('initial' => array(
                 'permissions' => \PFF\Arr::create($role->permissions())->pluck('id')->arr(),
             )));
         else
-            return new IPFAdminRoleForm($data, null);
+            return new RoleForm($data, null);
     }
 
     public function saveObject($form, $role)
@@ -315,8 +320,8 @@ class AdminRole extends IPF_Admin_Component
         $role->save();
 
         Permission::revokeAll($role);
-        foreach (Permission::query()->where('id', $form->cleaned_data['permissions']) as $permission)
-            $permission->grant($role);
+        $permissions = Permission::query()->where('id', $form->cleaned_data['permissions'])->fetchAll();
+        Permission::grantAll($permissions, $role);
 
         return array($role->id, $role);
     }
@@ -331,16 +336,17 @@ class AdminRole extends IPF_Admin_Component
 
     public function page_title()   { return __('Role'); }
     public function verbose_name() { return __('Role'); }
+    public function slug() { return 'role'; }
 }
 
-if (IPF_Auth_App::ArePermissionsEnabled()) {
+if (\IPF_Auth_App::ArePermissionsEnabled()) {
     return array(
-        'AdminUser',
-        'AdminRole',
+        'IPF\Auth\Admin\AdminUser',
+        'IPF\Auth\Admin\AdminRole',
     );
 } else {
     return array(
-        'AdminUser',
+        'IPF\Auth\Admin\AdminUser',
     );
 }
 
index 3e3049edaa4787f7bcb1020887b2770beb4a6cbe..e7f89a22e992726614e14467bf43181838e4f0cd 100644 (file)
@@ -6,7 +6,7 @@ class IPF_Auth_App extends IPF_Application
 
     function configure($settings)
     {
-        $this->userModel = \PFF\Arr::get($settings, 'auth_user_model', 'User');
+        $this->userModel = \PFF\Arr::get($settings, 'auth_user_model', 'IPF\\Auth\\User');
         \PFF\Container::set('auth', $this);
     }
 
@@ -49,7 +49,7 @@ class IPF_Auth_App extends IPF_Application
 
     function logout($request)
     {
-        $request->user = new AnonymousUser;
+        $request->user = new \IPF\Auth\AnonymousUser;
         $request->session->data = array(
             'login_time' => gmdate('Y-m-d H:i:s')
         );
@@ -70,7 +70,7 @@ class IPF_Auth_App extends IPF_Application
     public function commands()
     {
         return array(
-            new IPF_Auth_Command_CreateSuperUser,
+            new \IPF\Auth\Commands\CreateSuperUser,
         );
     }
 }
index 696b8974a9173beb19f7cf5d7f30fd0af7d885e0..fc444605872622dda5c014aa6b78adf49df4079d 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 
-class IPF_Auth_Command_CreateSuperUser
+namespace IPF\Auth\Commands;
+
+class CreateSuperUser
 {
     public $command = 'createsuperuser';
     public $description = 'Create superuser';
index 90c6d155eb375ec247eca759fc398695f6f4014d..c50d5c2b209504139976fbaea748f12b3e53e7be 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 
-class IPF_Auth_Middleware
+namespace IPF\Auth;
+
+class Middleware
 {
     const SessionKey = 'IPF_User_auth';
 
@@ -11,14 +13,14 @@ class IPF_Auth_Middleware
         $user_id = \PFF\Arr::get($request->session->data, self::SessionKey, 0);
         if ($user_id > 0) {
             $request->user = \PFF\Container::auth()->findUser($user_id);
-            if ($request->user && 43200 < IPF_Utils::dateCompare($request->user->last_login)) {
+            if ($request->user && 43200 < \IPF_Utils::dateCompare($request->user->last_login)) {
                 $request->user->last_login = gmdate('Y-m-d H:i:s');
                 $request->user->save();
             }
         }
 
         if (!$request->user)
-            $request->user = new AnonymousUser;
+            $request->user = new \IPF\Auth\AnonymousUser;
 
         return false;
     }
index a2e75c72f9d702513506c3e9f91291d6b04b8a26..41490e994129e367592401f5ebf57b229ddce026 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+namespace IPF\Auth;
+
 abstract class DBObject
 {
     public $id;
@@ -7,10 +9,10 @@ abstract class DBObject
 
     public function save()
     {
-        $r = new ReflectionObject($this);
+        $r = new \ReflectionObject($this);
 
         $values = array();
-        foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $p) {
+        foreach ($r->getProperties(\ReflectionProperty::IS_PUBLIC) as $p) {
             $name = $p->getName();
             if ($name === 'id' || $name[0] === '_')
                 continue;
@@ -65,14 +67,14 @@ class User extends DBObject
     function setPassword($raw_password)
     {
         if ($raw_password)
-            $this->password = IPF_Crypto::hashPassword($raw_password);
+            $this->password = \IPF_Crypto::hashPassword($raw_password);
         else
             $this->password = '';
     }
 
     function checkPassword($raw_password)
     {
-        return $this->is_active && IPF_Crypto::checkPassword($raw_password, $this->password);
+        return $this->is_active && \IPF_Crypto::checkPassword($raw_password, $this->password);
     }
 
     function isAnonymous()
@@ -125,7 +127,7 @@ class User extends DBObject
                 $cond[] = 'name = ?';
                 $params[] = $permission;
             } else {
-                throw new IPF_Exception('Bad argument');
+                throw new \IPF_Exception('Bad argument');
             }
         }
 
@@ -150,7 +152,7 @@ class Role extends DBObject
     {
         return \PFF\Container::databaseQuery()
             ->from('auth_role')
-            ->asObject('Role')
+            ->asObject('IPF\\Auth\\Role')
             ->orderBy('name');
     }
 
@@ -197,7 +199,7 @@ class Permission extends DBObject
     {
         return \PFF\Container::databaseQuery()
             ->from('auth_permission')
-            ->asObject('Permission')
+            ->asObject('IPF\\Auth\\Permission')
             ->orderBy('name');
     }
 
@@ -241,7 +243,7 @@ class Permission extends DBObject
         } elseif ($obj instanceof Role) {
             return array('auth_role_permission', array('role_id' => $obj->id));
         } else {
-            throw new IPF_Exception('Bad argument.');
+            throw new \IPF_Exception('Bad argument.');
         }
     }
 }