Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * 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;
 }
Example #3
0
 /**
  * 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!');
 }
Example #4
0
 /**
  * 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;
 }