/** * @param Form $form */ public function Submit(Form $form) { $json = new \stdClass(); $json->result = "success"; $values = $form->getValues(); $result = false; if (!empty($values['userID'])) { $userEntity = $this->userRepository->get($values['userID']); if ($userEntity) { if ($this->user->isAllowed("user_management", "edit")) { $userEntity->setLogin($values['login']); if (!empty($values['password1'])) { $userEntity->setPassword($values['password1']); } if ($userEntity->getLogin() !== "root" && $userEntity->getUserID() !== $this->user->getId()) { $userEntity->setActive($values['active']); if ($userEntity->getRole()->getAclRoleID() != $this->user->getIdentity()->data['aclRoleID']) { $userEntity->setAclRoleID($values['role']); } } try { $result = $this->userRepository->save(); } catch (\PDOException $e) { $result = $e->getMessage(); } } else { $result = UserForm::PERMISSION; } } } else { if ($this->user->isAllowed("user_management", "add")) { $userEntity = new UserEntity(); $userEntity->setLogin($values['login'])->setPassword($values['password1'])->setActive($values['active'])->setAclRoleID($values['role']); try { $result = $this->userRepository->push($userEntity)->save(); } catch (\PDOException $e) { $result = $e->getMessage(); if (preg_match("/Duplicate entry/", $result)) { $result = "Nick <strong>" . $values['login'] . "</strong> již existuje. Zvolte prosím jiný login."; } } if ($result instanceof UserEntity || $result === TRUE) { $result = TRUE; } } else { $result = UserForm::PERMISSION; } } if ($result === TRUE) { $json->result = "success"; } else { $json->result = "error"; $json->message = $result; } $response = new JsonResponse($json); $this->getPresenter()->sendResponse($response); }
/** * Performs an authentication * @param array $credentials * @return NS\Identity * @throws NS\AuthenticationException */ public function authenticate(array $credentials) { list($username, $password) = $credentials; $userSel = $this->users->read(); $userSel->where('login', $username)->where("active", TRUE); $user = $userSel->fetch(); if (!$user) { throw new NS\AuthenticationException("User '{$username}' not found.", self::IDENTITY_NOT_FOUND); } if ($user->getPassword() !== $user->calculateHash($password)) { throw new NS\AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL); } $user->setLastLogged(new DateTime()); $user->setIp($_SERVER['REMOTE_ADDR']); $this->users->save(); $data = $user->toArray(); unset($data['password']); return new NS\Identity($user->getUserID(), $user->getRole()->getName(), $data); }