From: Andrey Kutejko Date: Sun, 14 Sep 2014 08:45:03 +0000 (+0300) Subject: move auth app code to namespace X-Git-Tag: 0.6~136 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=82f3b084e3de962b3a7f9736e66701b385de44f8;p=ipf.git move auth app code to namespace --- diff --git a/ipf/auth/admin.php b/ipf/auth/admin.php index e45bb74..69ff322 100644 --- a/ipf/auth/admin.php +++ b/ipf/auth/admin.php @@ -1,5 +1,9 @@ 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', ); } diff --git a/ipf/auth/app.php b/ipf/auth/app.php index 3e3049e..e7f89a2 100644 --- a/ipf/auth/app.php +++ b/ipf/auth/app.php @@ -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, ); } } diff --git a/ipf/auth/commands/createsuperuser.php b/ipf/auth/commands/createsuperuser.php index 696b897..fc44460 100644 --- a/ipf/auth/commands/createsuperuser.php +++ b/ipf/auth/commands/createsuperuser.php @@ -1,6 +1,8 @@ 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; } diff --git a/ipf/auth/models.php b/ipf/auth/models.php index a2e75c7..41490e9 100644 --- a/ipf/auth/models.php +++ b/ipf/auth/models.php @@ -1,5 +1,7 @@ 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.'); } } }