/** * Add elements to form. * * @return void */ public function initialize() { $this->setTitle('User Creation')->setDescription('Create new user.')->setAttribute('autocomplete', 'off'); $content = $this->addContentFieldSet()->addText('username', null, null, null, [], ['autocomplete' => 'off'])->addPassword('password', null, null, [], ['autocomplete' => 'off'])->addText('email', null, null, null, [], ['autocomplete' => 'off'])->addSelect('role_id', 'Role', 'Select user role', Role::find(), null, ['using' => ['id', 'name']]); $this->addFooterFieldSet()->addButton('create')->addButtonLink('cancel', 'Cancel', ['for' => 'admin-users']); $this->_setValidation($content); }
/** * Initialize form. * * @return void */ public function initialize() { $this->setDescription('This menu item will be available under menu or parent menu item.'); $content = $this->addContentFieldSet()->addText('title')->addSelect('target', 'Target', 'Link type', [null => 'Default link', MenuItem::ITEM_TARGET_BLANK => 'Opens the linked document in a new window or tab', MenuItem::ITEM_TARGET_PARENT => 'Opens the linked document in the parent frame', MenuItem::ITEM_TARGET_TOP => 'Opens the linked document in the full body of the window'])->addRadio('url_type', 'Select url type', null, [0 => 'Url', 1 => 'System page'])->addText('url', 'Url', 'Do not type url with starting slash... Example: "somepage/url/to?param=1"')->addText('page', 'Page', 'Start typing to see pages variants.', null, [], ['data-link' => $this->getDI()->getUrl()->get('admin/pages/suggest'), 'data-target' => '#page_id', 'data-widget' => 'autocomplete', 'autocomplete' => 'off'])->addTextArea('onclick', 'OnClick', 'Type JS action that will be performed when this menu item is selected.')->addCkEditor('tooltip')->addSelect('tooltip_position', 'Tooltip position', null, [MenuItem::ITEM_TOOLTIP_POSITION_TOP => 'Top', MenuItem::ITEM_TOOLTIP_POSITION_BOTTOM => 'Bottom', MenuItem::ITEM_TOOLTIP_POSITION_LEFT => 'Left', MenuItem::ITEM_TOOLTIP_POSITION_RIGHT => 'Right'])->addRemoteFile('icon', 'Select icon')->addSelect('icon_position', 'Icon position', null, [MenuItem::ITEM_ICON_POSITION_LEFT => 'Left', MenuItem::ITEM_ICON_POSITION_RIGHT => 'Right'])->addMultiSelect('languages', 'Languages', 'Choose the language in which the menu item will be displayed. If no one selected - will be displayed at all.', Language::find(), null, ['using' => ['language', 'name']])->addMultiSelect('roles', 'Roles', 'If no value is selected, will be allowed to all (also as all selected).', Role::find(), null, ['using' => ['id', 'name']])->addCheckbox('is_enabled', 'Is enabled', null, 1, true, false)->addHidden('page_id')->addHidden('menu_id')->addHidden('parent_id'); $this->_setValidation($content); }
/** * Initialize form. * * @return void */ public function initialize() { $this->setTitle('Page Creation')->setDescription('Create new page.'); $content = $this->addContentFieldSet()->addText('title')->addText('url', 'Url', 'Page will be available under http://' . $_SERVER['HTTP_HOST'] . '/page/[URL NAME]')->addTextArea('description')->addTextArea('keywords')->addTextArea('controller', 'Controller', 'Controller and action name that will handle this page. Example: NameController->someAction', null, ['emptyAllowed' => true, 'escape' => false])->addMultiSelect('roles', 'Roles', 'If no value is selected, will be allowed to all (also as all selected).', Role::find(), null, ['using' => ['id', 'name']]); $this->addFooterFieldSet()->addButton('create')->addButtonLink('cancel', 'Cancel', ['for' => 'admin-pages']); $this->_setValidation($content); }
/** * Initialize grid columns. * * @return array */ protected function _initColumns() { $this->addTextColumn('u.id', 'ID', [self::COLUMN_PARAM_TYPE => Column::BIND_PARAM_INT, self::COLUMN_PARAM_OUTPUT_LOGIC => function (GridItem $item, $di) { $url = $di->get('url')->get(['for' => 'admin-users-view', 'id' => $item['u.id']]); return sprintf('<a href="%s">%s</a>', $url, $item['u.id']); }])->addTextColumn('u.username', 'Username')->addTextColumn('u.email', 'Email')->addSelectColumn('r.name', 'Role', ['hasEmptyValue' => true, 'using' => ['name', 'name'], 'elementOptions' => Role::find()], [self::COLUMN_PARAM_USE_HAVING => false, self::COLUMN_PARAM_USE_LIKE => false, self::COLUMN_PARAM_OUTPUT_LOGIC => function (GridItem $item) { return $item['name']; }])->addTextColumn('u.creation_date', 'Creation Date'); }
/** * Get default guest role. * * @return Role */ public static function getDefaultRole() { $role = Role::findFirst(["is_default = 1", 'cache' => ['key' => self::CACHE_KEY_ROLE_DEFAULT]]); if (!$role) { $role = new Role(); $role->name = "User"; $role->description = 'Default user role.'; $role->type = 'user'; $role->undeletable = 1; $role->save(); } return $role; }
/** * Get widget cache key. * * @return string|null */ public function getCacheKey() { $key = self::CACHE_PREFIX; $role = User::getViewer()->getRole(); if ($role) { $key .= $role->type; } else { $key .= Role::getRoleByType(Acl::DEFAULT_ROLE_GUEST)->type; } $key .= '_' . $this->getDI()->getSession()->get('language'); return $key; }
/** * Edit access. * * @param int $id Identity. * * @return ResponseInterface|mixed|void * * @Route("/edit/{id:[a-zA-Z_-]+}", methods={"GET", "POST"}, name="admin-access-edit") */ public function editAction($id) { // Normalize id. $urlId = $id; $id = str_replace('_', '\\', $id); // Check current role change request. $changeRole = $this->request->get('role'); if ($changeRole !== null) { $this->session->set('admin-current-role', $changeRole); return $this->response->redirect(['for' => 'admin-access-edit', 'id' => $urlId]); } $resources = $this->core->acl()->getResources(); $resourceFound = false; foreach ($resources as $resource) { if ($resource->getName() == $id) { $resourceFound = true; break; } } if (!$resourceFound) { return $this->response->redirect(['for' => 'admin-access']); } // get all roles and current $roles = Role::find(); $currentRole = $this->session->get('admin-current-role'); $currentRole = Role::findFirst($currentRole); if (!$currentRole) { $currentRole = Role::getRoleByType(Acl::DEFAULT_ROLE_ADMIN); } $objectAcl = $this->core->acl()->getObject($id); $form = $this->_getForm($objectAcl, $currentRole); $this->view->currentObject = $id; $this->view->form = $form; $this->view->roles = $roles; $this->view->currentRole = $currentRole; if (!$this->request->isPost() || !$form->isValid()) { return; } $data = $form->getValues(); // save actions foreach ($objectAcl->actions as $action) { $result = Access::findFirst(["conditions" => "object = ?1 AND action = ?2 AND role_id = ?3", "bind" => [1 => $id, 2 => $action, 3 => $currentRole->id]]); if (!$result) { $result = new Access(); $result->object = $id; $result->action = $action; $result->role_id = $currentRole->id; } if (empty($data[$action])) { $result->value = 'deny'; } else { $result->value = 'allow'; } $result->save(); } //save options foreach ($objectAcl->options as $options) { $result = Access::findFirst(["conditions" => "object = ?1 AND action = ?2 AND role_id = ?3", "bind" => [1 => $id, 2 => $options, 3 => $currentRole->id]]); if (!$result) { $result = new Access(); $result->object = $id; $result->action = $options; $result->role_id = $currentRole->id; } if (empty($data[$options])) { $data[$options] = null; } $result->value = $data[$options]; $result->save(); } $this->core->acl()->clearAcl(); $this->flash->success('Settings saved!'); }
/** * Delete role. * * @param int $id Role identity. * * @return mixed * * @Get("/roles-delete/{id:[0-9]+}", name="admin-roles-delete") */ public function rolesDeleteAction($id) { $item = Role::findFirst($id); if ($item) { if ($item->is_default) { $anotherRole = Role::findFirst(); if ($anotherRole) { $anotherRole->is_default = 1; $anotherRole->save(); } } if ($item->delete()) { $this->flashSession->notice('Object deleted!'); } else { $this->flashSession->error($item->getMessages()); } } return $this->response->redirect(['for' => 'admin-users-roles']); }
/** * Get current user * If user logged in this function will return user object with data * If user isn't logged in this function will return empty user object with ID = 0 * * @return User */ public static function getViewer() { if (null === self::$_viewer) { $identity = DI::getDefault()->get('core')->auth()->getIdentity(); if ($identity) { self::$_viewer = self::findFirst($identity); } if (!self::$_viewer) { self::$_viewer = new User(); self::$_viewer->id = 0; self::$_viewer->role_id = Role::getRoleByType(Acl::DEFAULT_ROLE_GUEST)->id; } } return self::$_viewer; }
/** * Installation finish. * * @return mixed * * @Route("/finish", methods={"GET", "POST"}, name="install-finish") */ public function finishAction() { if (!$this->_isPassed('databaseAction')) { return $this->_selectAction(); } $form = new FinishForm(); if ($this->request->isPost() && $form->isValid()) { $password = $this->request->getPost('password', 'string'); $repeatPassword = $this->request->getPost('repeatPassword', 'string'); if ($password != $repeatPassword) { $form->addError("Passwords doesn't match!"); $this->view->form = $form; return; } // Setup database. $this->_setupDatabase(); $user = new User(); $data = $form->getValues(); $user->role_id = Role::getRoleByType('admin')->id; if (!$user->save($data)) { foreach ($user->getMessages() as $message) { $form->addError($message); } $this->view->form = $form; return; } $this->_setPassed(__FUNCTION__, true); return $this->response->redirect(['for' => 'install-save']); } $this->view->form = $form; }
/** * Widget options. * * @return void * * @Route("/widget-options", methods={"GET", "POST"}, name="admin-pages-widget-options") */ public function widgetOptionsAction() { $widgetIndex = $this->request->get('widget_index', 'int', -1); if ($widgetIndex != '0' && intval($widgetIndex) == 0) { $widgetIndex = -1; } $currentPageWidgets = $this->session->get('admin-pages-manage', []); if ($widgetIndex == -1) { $widgetIndex = $this->session->get('admin-pages-widget-index'); $currentPageWidgets[$widgetIndex] = ['widget_index' => $widgetIndex, 'id' => 0, 'layout' => $this->request->get('layout', 'string', 'middle'), 'widget_id' => $this->request->get('widget_id', 'int'), 'params' => []]; } if (empty($currentPageWidgets[$widgetIndex])) { return; } $widgetData = $currentPageWidgets[$widgetIndex]; $id = $widgetData['id']; $widgetParams = $widgetData['params']; $widgetParams['content_id'] = $id; $widget_id = $widgetData['widget_id']; $widgetMetadata = Widget::findFirstById($widget_id); $form = new CoreForm(); // building widget form $adminForm = $widgetMetadata->admin_form; if (empty($adminForm)) { $form->addText('title'); } elseif ($adminForm == 'action') { $widgetName = $widgetMetadata->name; if ($widgetMetadata->module !== null) { $widgetClass = '\\' . ucfirst($widgetMetadata->module) . '\\Widget\\' . $widgetName . '\\Controller'; } else { $widgetClass = '\\Widget\\' . $widgetName . '\\Controller'; } $widgetController = new $widgetClass(); $widgetController->setDefaults($widgetName, ucfirst($widgetMetadata->module), $widgetParams); $widgetController->prepare(); $form = $widgetController->adminAction(); } else { $form = new $adminForm(); } if ($widgetMetadata->is_paginated == 1) { $form->addText('count', 'Items count', null, 10); $form->setOrder('count', 1000); } if ($widgetMetadata->is_acl_controlled == 1) { $form->addMultiSelect('roles', 'Roles', null, Role::find(), null, ['using' => ['id', 'name']]); $form->setOrder('roles[]', 1001); } // set form values if (!empty($widgetParams)) { $form->setValues($widgetParams); } if (!$this->request->isPost() || !$form->isValid()) { $this->view->form = $form; $this->view->id = $id; $this->view->name = $widgetMetadata->name; return; } $currentPageWidgets[$widgetIndex]['params'] = $form->getValues(); $this->resolveModal(['hide' => true, 'customJs' => 'setEditedWidgetIndex(' . $widgetIndex . ');']); $this->session->set('admin-pages-manage', $currentPageWidgets); $this->session->set('admin-pages-widget-index', ++$widgetIndex); }
/** * Register action. * * @return mixed * * @Route("/register", methods={"GET", "POST"}, name="register") */ public function registerAction() { if (User::getViewer()->id) { return $this->response->redirect(); } $form = new RegisterForm(); if (!$this->request->isPost() || !$form->isValid()) { $this->view->form = $form; return; } $password = $form->getValue('password'); $repeatPassword = $form->getValue('repeatPassword'); if ($password != $repeatPassword) { $form->addError("Passwords doesn't match!", 'password'); $this->view->form = $form; return; } $user = new User(); $data = $form->getValues(); $user->role_id = Role::getDefaultRole()->id; if (!$user->save($data)) { foreach ($user->getMessages() as $message) { $form->addError($message); } $this->view->form = $form; return; } $this->core->auth()->authenticate($user->id); return $this->response->redirect(); }