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 {
function logout()
{
- \PFF\Container::auth()->logout($this->request);
+ $this->authApp()->logout($this->request);
$context = array(
'page_title' => IPF::get('admin_title'),
);
{
$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);
}
}
-
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(
));
$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'),
));
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'));
}
}
-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');
public function itemsCount($searchValue, $filters)
{
- return $this->auth_app->userQuery()
+ return $this->authApp()->userQuery()
->select(null)->select('COUNT(1)')
->fetchColumn();
}
'is_staff',
'is_superuser',
);
- if ($this->auth_app->arePermissionsEnabled()) {
+ if ($this->authApp()->arePermissionsEnabled()) {
$columns[] = 'groups';
}
return $columns;
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'),
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);
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)),
);
}
{
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'),
));
}
}
-class AdminRole extends \IPF_Admin_Component
+class AdminRole extends Component
{
public function getItems($searchValue, $filters, $page, $pageSize)
{
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)
{
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;