]> git.andy128k.dev Git - ipf.git/commitdiff
reduce amount of static calls
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Dec 2016 21:54:27 +0000 (22:54 +0100)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Dec 2016 21:54:27 +0000 (22:54 +0100)
ipf/admin/controllers/file_browser.php
ipf/admin/controllers/user.php
ipf/auth/admin.php
ipf/middleware/serve_static.php

index ecad3bcf5547c884bd19e68e06f527f6864647da..0b3d6e1f7c2289a74b0592a5cbc98e8b67a6aae4 100644 (file)
@@ -127,7 +127,8 @@ class IPF_Admin_FileBrowser_Controller extends IPF_Admin_Base_Controller
 
     protected function backToIndex()
     {
-        return new IPF_HTTP_Response_Redirect(IPF_HTTP_URL::urlForView(array('IPF_Admin_FileBrowser_Controller', 'index')) . '?dir=' . $this->relative);
+        $url = $this->project->router->reverse(array('IPF_Admin_FileBrowser_Controller', 'index')) . '?dir=' . urlencode($this->relative);
+        return new IPF_HTTP_Response_Redirect($url);
     }
 
     function rename()
index bfbb50bee34525d821faf77b6f0df229d70b5cba..00319fa47a54bdc7e01960bbca0ff06eecc0f7ab 100644 (file)
@@ -2,16 +2,28 @@
 
 class IPF_Admin_User_Controller extends IPF_Admin_Base_Controller
 {
+    /**
+     * @return IPF_Auth_App
+     * @throws Exception
+     */
+    private function authApp()
+    {
+        foreach ($this->project->appList() as $app)
+            if ($app instanceof IPF_Auth_App)
+                return $app;
+        throw new Exception('Auth app is not registered in project.');
+    }
+
     function login()
     {
         $success_url = trim(\PFF\Arr::get($this->request->REQUEST, 'next', ''));
         if (!$success_url)
-            $success_url = IPF_HTTP_URL::urlForView(array('IPF_Admin_Dashboard_Controller', 'index'));
+            $success_url = $this->project->router->reverse(array('IPF_Admin_Dashboard_Controller', 'index'));
 
         if ($this->request->method == 'POST') {
             $form = new IPF_Admin_Forms_Login($this->request->POST);
             if ($form->isValid()) {
-                \PFF\Container::auth()->login($this->request, $form->user);
+                $this->authApp()->login($this->request, $form->user);
                 return new IPF_HTTP_Response_Redirect($success_url);
             }
         } else {
@@ -27,7 +39,7 @@ class IPF_Admin_User_Controller extends IPF_Admin_Base_Controller
 
     function logout()
     {
-        \PFF\Container::auth()->logout($this->request);
+        $this->authApp()->logout($this->request);
         $context = array(
             'page_title' => IPF::get('admin_title'),
         );
@@ -38,16 +50,15 @@ class IPF_Admin_User_Controller extends IPF_Admin_Base_Controller
     {
         $success_url = trim(\PFF\Arr::get($this->request->REQUEST, 'next', ''));
         if (!$success_url)
-            $success_url = IPF_HTTP_URL::urlForView(array('IPF_Admin_Dashboard_Controller', 'index'));
+            $success_url = $this->project->router->reverse(array('IPF_Admin_Dashboard_Controller', 'index'));
 
         if (!$this->request->user->isAnonymous() && $this->request->user->is_superuser) {
-            $user = \PFF\Container::auth()->findUser($this->params[1]);
+            $user = $this->authApp()->findUser($this->params[1]);
             if (!$user)
                 return new IPF_HTTP_Response_NotFound($this->request);
-            \PFF\Container::auth()->login($this->request, $user);
+            $this->authApp()->login($this->request, $user);
         }
 
         return new IPF_HTTP_Response_Redirect($success_url);
     }
 }
-
index 831749a73350d1a02166c5c31a64c5bcb435c656..c814c54dc70f9be360fed433d812b32f8897a148 100644 (file)
@@ -2,39 +2,23 @@
 
 namespace IPF\Auth\Admin;
 
+use IPF_Admin_App;
+use IPF_Auth_App;
 use IPF_Project;
 use IPF_Application;
 use \IPF\Auth\Role as Role;
 use \IPF\Auth\Permission as Permission;
 use \PFF\HtmlBuilder\Text as Text;
 
-function permissionChoices()
-{
-    /** @var \IPF_Admin_App $admin_app */
-    $admin_app = \IPF_Project::getInstance()->getApplicationBySlug('admin');
-
-    $choices = array();
-    foreach (Permission::query()->fetchAll() as $p) {
-        list($appSlug, $componentSlug, $permission) = explode('|', $p->name);
-        $component = $admin_app->getComponentBySlugs($appSlug, $componentSlug);
-
-        if ($component)
-            $title = $component->app->getTitle().' | '.$component->verbose_name().' | '.ucfirst($permission);
-        else
-            $title = $p->name;
-
-        $choices[$title] = $p->id;
-    }
-    ksort($choices);
-    return $choices;
-}
-
 class UserForm extends \IPF_ObjectForm
 {
     private $isAdd;
 
     function initFields($extra=array())
     {
+        $auth_app = $extra['auth_app'];
+        $permissions_choices = $extra['permissions_choices'];
+
         $this->isAdd = $extra['is_add'];
 
         $this->fields['username'] = new \IPF_Form_Field_Regex(array(
@@ -82,13 +66,13 @@ class UserForm extends \IPF_ObjectForm
         ));
 
         $permissions = array('is_active', 'is_staff', 'is_superuser');
-        if (\PFF\Container::auth()->arePermissionsEnabled()) {
+        if ($auth_app->arePermissionsEnabled()) {
             $permissions[] = 'permissions';
             $permissions[] = 'roles';
 
             $this->fields['permissions'] = new \IPF_Form_Field_MultipleChoice(array(
                 'label'     => __('Permissions'),
-                'choices'   => permissionChoices(),
+                'choices'   => $permissions_choices,
                 'widget'    => 'IPF_Form_Widget_SelectMultipleInputCheckbox',
                 'widget_attrs' => array('class' => 'checkgroup'),
             ));
@@ -107,7 +91,7 @@ class UserForm extends \IPF_ObjectForm
             array('fields' => $permissions, 'label' => __('Permissions')),
         );
 
-        $extra_fields = call_user_func(array(\PFF\Container::auth()->userModel, 'adminExtraFields'));
+        $extra_fields = call_user_func(array($auth_app->userModel, 'adminExtraFields'));
         if ($extra_fields) {
             $this->fields = array_merge($this->fields, $extra_fields);
             $this->field_groups[] = array('fields' => array_keys($extra_fields), 'label' => __('Extra'));
@@ -127,18 +111,61 @@ class UserForm extends \IPF_ObjectForm
     }
 }
 
-class AdminUser extends \IPF_Admin_Component
+abstract class Component extends \IPF_Admin_Component
 {
+    /** @var IPF_Project */
+    protected $project;
+
+    /** @var IPF_Auth_App */
     private $auth_app;
 
-    function __construct()
+    function __construct(IPF_Project $project, IPF_Auth_App $auth_app)
+    {
+        $this->project = $project;
+        $this->auth_app = $auth_app;
+    }
+
+    /**
+     * @return IPF_Auth_App
+     */
+    protected function authApp()
     {
-        $this->auth_app = \PFF\Container::auth();
+        return $this->auth_app;
+    }
+
+    protected function permissionChoices()
+    {
+        $admin_app = null;
+        foreach ($this->project->appList() as $a)
+            if ($a instanceof IPF_Admin_App)
+                $admin_app = $a;
+
+        if (!$admin_app)
+            throw new \Exception('Admin application is not registered.');
+
+        $choices = array();
+        foreach (Permission::query()->fetchAll() as $p) {
+            list($appSlug, $componentSlug, $permission) = explode('|', $p->name);
+            $component = $admin_app->getComponentBySlugs($appSlug, $componentSlug);
+
+            if ($component)
+                $title = $component->app->getTitle() . ' | ' . $component->verbose_name() . ' | ' . ucfirst($permission);
+            else
+                $title = $p->name;
+
+            $choices[$title] = $p->id;
+        }
+        ksort($choices);
+        return $choices;
+
     }
+}
 
+class AdminUser extends Component
+{
     public function getItems($searchValue, $filters, $page, $pageSize)
     {
-        return $this->auth_app->userQuery()
+        return $this->authApp()->userQuery()
             ->limit($pageSize)
             ->offset(($page - 1) * $pageSize)
             ->fetchAll('id');
@@ -146,7 +173,7 @@ class AdminUser extends \IPF_Admin_Component
 
     public function itemsCount($searchValue, $filters)
     {
-        return $this->auth_app->userQuery()
+        return $this->authApp()->userQuery()
             ->select(null)->select('COUNT(1)')
             ->fetchColumn();
     }
@@ -160,7 +187,7 @@ class AdminUser extends \IPF_Admin_Component
             'is_staff',
             'is_superuser',
         );
-        if ($this->auth_app->arePermissionsEnabled()) {
+        if ($this->authApp()->arePermissionsEnabled()) {
             $columns[] = 'groups';
         }
         return $columns;
@@ -193,12 +220,15 @@ class AdminUser extends \IPF_Admin_Component
 
     public function getObjectByID($id)
     {
-        return \PFF\Container::auth()->findUser($id);
+        return $this->authApp()->findUser($id);
     }
 
     protected function _getForm($user, $data)
     {
-        $extra = array();
+        $extra = array(
+            'auth_app' => $this->authApp(),
+            'permissions_choices' => $this->permissionChoices(),
+        );
         if ($user) {
             $extra['initial'] = array(
                 'permissions' => \PFF\Arr::pluck($user->permissions(), 'id'),
@@ -214,14 +244,14 @@ class AdminUser extends \IPF_Admin_Component
     public function saveObject($form, $user)
     {
         if (!$user)
-            $user = $this->auth_app->createUser();
+            $user = $this->authApp()->createUser();
 
         $form->toObject($user);
         if ($form->cleaned_data['password1'])
             $user->setPassword($form->cleaned_data['password1']);
         $user->save();
 
-        if (\PFF\Container::auth()->arePermissionsEnabled()) {
+        if ($this->authApp()->arePermissionsEnabled()) {
             Permission::revokeAll($user);
             $permissions = Permission::query()->where('id', $form->cleaned_data['permissions'])->fetchAll();
             Permission::grantAll($permissions, $user);
@@ -244,7 +274,7 @@ class AdminUser extends \IPF_Admin_Component
     protected function objectTools($user)
     {
         return array(
-            'impersonate' => \IPF_HTTP_URL::urlForView(array('IPF_Admin_User_Controller', 'impersonate'), array($user->id)),
+            'impersonate' => $this->project->router->reverse(array('IPF_Admin_User_Controller', 'impersonate'), array($user->id)),
         );
     }
 
@@ -256,16 +286,19 @@ class RoleForm extends \IPF_ObjectForm
 {
     function initFields($extra=array())
     {
+        $auth_app = $extra['auth_app'];
+        $permissions_choices = $extra['permissions_choices'];
+
         $this->fields['name'] = new \IPF_Form_Field_Varchar(array(
             'required' => true,
             'label' => __('Name'),
             'max_length' => 255,
         ));
 
-        if (\PFF\Container::auth()->arePermissionsEnabled()) {
+        if ($auth_app->arePermissionsEnabled()) {
             $this->fields['permissions'] = new \IPF_Form_Field_MultipleChoice(array(
                 'label'     => __('Permissions'),
-                'choices'   => permissionChoices(),
+                'choices'   => $permissions_choices,
                 'widget'    => 'IPF_Form_Widget_SelectMultipleInputCheckbox',
                 'widget_attrs' => array('class' => 'checkgroup'),
             ));
@@ -273,7 +306,7 @@ class RoleForm extends \IPF_ObjectForm
     }
 }
 
-class AdminRole extends \IPF_Admin_Component
+class AdminRole extends Component
 {
     public function getItems($searchValue, $filters, $page, $pageSize)
     {
@@ -316,12 +349,19 @@ class AdminRole extends \IPF_Admin_Component
 
     protected function _getForm($role, $data)
     {
-        if ($role)
-            return new RoleForm($data, $role, array('initial' => array(
+        $extra = array(
+            'auth_app' => $this->authApp(),
+            'permissions_choices' => $this->permissionChoices(),
+        );
+
+        if ($role) {
+            $extra['initial'] = array(
                 'permissions' => \PFF\Arr::pluck($role->permissions(), 'id'),
-            )));
-        else
-            return new RoleForm($data, null);
+            );
+            return new RoleForm($data, $role, $extra);
+        } else {
+            return new RoleForm($data, null, $extra);
+        }
     }
 
     public function saveObject($form, $role)
@@ -356,12 +396,14 @@ class AuthAdminSection implements \IPF_Admin_ISection
 {
     public function components(IPF_Project $project, IPF_Application $app)
     {
+        /** @var IPF_Auth_App $app */
+
         $components = array(
-            new AdminUser(),
+            new AdminUser($project, $app),
         );
 
         if ($app->arePermissionsEnabled()) {
-            $components[] = new AdminRole();
+            $components[] = new AdminRole($project, $app);
         }
 
         return $components;
index 7e32002212ebcc332739b1a538f0a61a47a7e4c4..b40e9dd0ae4110c63738c12b6a3b0cacaa708f27 100644 (file)
@@ -11,7 +11,7 @@ class IPF_Serve_Static_Middleware extends IPF_Middleware
 
         $query = $matches[1];
 
-        foreach (IPF_Project::getInstance()->appList() as $app) {
+        foreach ($this->project->appList() as $app) {
             $static = $app->getPath() . $staticUrl . $query;
             if (is_file($static))
                 return new IPF_HTTP_Response_File($static, null, $this->mimetype($query));