Inheritance: use trait Nette\StaticClass
Example #1
0
 /**
  * Performs an authentication against e.g. database.
  * and returns IIdentity on success or throws AuthenticationException
  * @var array $credentials
  * @return Nette\Security\IIdentity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     if (is_null($password)) {
         $user = $this->manager->getItem($username);
     } else {
         $user = $this->manager->getUserByUsername($username);
         if (is_null($user->password)) {
             throw new Trejjam\Authorization\User\AuthenticatorException('User has not have defined password.', Trejjam\Authorization\User\AuthenticatorException::NOT_DEFINED_PASSWORD);
         } else {
             if (!Nette\Security\Passwords::verify($password, $user->password)) {
                 throw new Trejjam\Authorization\User\AuthenticatorException('The password is incorrect.', Trejjam\Authorization\User\AuthenticatorException::INVALID_CREDENTIAL);
             } else {
                 if (Nette\Security\Passwords::needsRehash($user->password)) {
                     $this->manager->changePassword($user, $password);
                 }
             }
         }
     }
     if ($this->manager->isEnableLogin($user)) {
         $baseUserArr = (array) $this->manager->getUserInfo($user);
         $baseUserArr["login_time"] = new Nette\Utils\DateTime();
         $role = $this->acl->getUserRoles($user);
         return new Identity($user->id, $role, $baseUserArr);
     } else {
         //this may not happen
         throw new Trejjam\Authorization\User\AuthenticatorException();
     }
 }
Example #2
0
 public function editUser($values, $user_id)
 {
     //        $temp = $this->database->table('user')->where('email = ?', $values->email)->fetch();
     $row = $this->database->table('user')->where('id', $user_id)->fetch();
     if (!NS\Passwords::verify($values->oldPassword, $row->password)) {
         //            throw new NS\AuthenticationException('Špatné heslo.');
         $check = 0;
     } else {
         if ($values->newPassword != NULL) {
             $this->database->table('user')->where('id', $user_id)->update(['password' => Passwords::hash($values->newPassword)]);
         }
         if ($values->username != NULL) {
             $this->database->table('user')->where('id', $user_id)->update(['username' => $values->username]);
         }
         $check = 1;
     }
     //        $check = 0;
     //        if ((!$temp)) $check = 1;
     //        if ($check) {
     //            $this->database->table('user')->where('id', $user_id)->update([
     //                'username' => $values->username,
     //                'password' => Passwords::hash($values->newPassword),
     //            ]);
     //
     //            /*$mail = new Message;
     //            $mail->setFrom('BrNOC bot <*****@*****.**>')
     //                ->addTo($values->email)
     //                ->setSubject('Potvrzení příhlášení')
     //                ->setBody("Byl jsi přihlášen jako účastník BrNOCi 2015. \n \nBrNOC tým");*/
     //        }
     return $check;
 }
 public function registerFormSucceeded($form, $values)
 {
     $hash = \Nette\Security\Passwords::hash($values['password']);
     $reguser = $this->database->table('users')->insert(array('username' => $values->username, 'password' => $hash, 'email' => $values->email));
     $this->flashMessage("Gratulujeme. Boli ste úspešne zaregistrovaný. Môžte sa prihlásiť do aplikácie.", 'success');
     $this->redirect('Sign:in');
 }
Example #4
0
 /**
  * Performs an authentication.
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     if (!Passwords::verify($credentials[1], $this->database->table('option')->get('password')->value)) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.', self::INVALID_CREDENTIAL);
     }
     return new Nette\Security\Identity(NULL, NULL, NULL);
 }
 /**
  * Performs an authentication.
  * @param array $credentials (string $username, string $password)
  * @return Nette\Security\Identity
  * @throws Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $row = $this->database->table('user')->where('username', $username)->fetch();
     if (!$row) {
         throw new Security\AuthenticationException('Uživatel s tímto jménem neexistuje.', self::IDENTITY_NOT_FOUND);
     } elseif (!Security\Passwords::verify($password, $row->password)) {
         throw new Security\AuthenticationException('Nesprávné heslo.', self::INVALID_CREDENTIAL);
     } elseif (!$row->active) {
         throw new Security\AuthenticationException('Účet není aktivovaný.', self::NOT_APPROVED);
     } elseif (Security\Passwords::needsRehash($row->password)) {
         $row->update(array('password' => Security\Passwords::hash($password)));
     }
     $arr = $row->toArray();
     unset($arr['password']);
     $roles = $row->related('privilege')->fetch()->toArray();
     unset($roles['user_id']);
     //adds privileges
     array_walk($roles, function (&$value, $key) use(&$roles) {
         if ($value != NULL) {
             $value = $key . ' - ' . $value;
         }
     });
     return new Security\Identity($row->id, $roles, $arr);
 }
Example #6
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @return void
  */
 public function add($username, $password)
 {
     $user = new Entities\UserEntity();
     $user->setUsername($username);
     $user->setPassword(Passwords::hash($password));
     $this->userRepository->save($user);
 }
 public function onSuccess()
 {
     $v = $this->values;
     $user = $this->orm->users->getByEmail($v->email);
     if ($user && $user->registered) {
         $this->addError('duplicate');
         return;
     }
     if (!$user) {
         $user = new User();
         $user->email = $v->email;
         $this->orm->users->attach($user);
     }
     $user->gender = $v->gender;
     $user->setNames($v->name);
     $user->registered = TRUE;
     $plainHash = Passwords::hash($v->password);
     $user->password = $this->aes->encrypt($plainHash);
     $this->orm->flush();
     /** @var Auth $presenter */
     $presenter = $this->presenter;
     $presenter->user->login(new Identity($user->id));
     $this->iLog('auth.registration.password', ['entropy' => $this->entropy->compute($v->password, $user)]);
     $presenter->onLogin($user, TRUE);
 }
Example #8
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @return void
  * @throws DuplicateNameException
  */
 public function add($username, $email, $password)
 {
     try {
         $this->database->table(self::TABLE_NAME)->insert([self::COLUMN_NAME => $username, self::COLUMN_PASSWORD_HASH => Passwords::hash($password), self::COLUMN_EMAIL => $email]);
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Example #9
0
 /**
  * Upraví
  * @param int
  * @param string
  * @param string
  * @param string
  * @return int
  */
 public function uprav($id, $jmeno, $heslo, $role)
 {
     $u = array(self::COLUMN_NAME => $jmeno, self::COLUMN_ROLE => $role);
     if ($heslo) {
         $u[self::COLUMN_PASSWORD_HASH] = Passwords::hash($heslo);
     }
     return $this->database->table(self::TABLE_NAME)->where(self::COLUMN_ID, (int) $id)->update($u);
 }
 public function hashPassword(ArrayHash $values)
 {
     if ($values->password) {
         $values->password = Passwords::hash($values->password);
     } else {
         unset($values->password);
     }
 }
Example #11
0
 /**
  * @param \stdClass $user
  * @throws DuplicateNameException
  */
 public function add(\stdClass $user)
 {
     try {
         $this->userModel->add(['username' => $user->username, 'email' => $user->email, 'password' => Passwords::hash($user->password), 'first_name' => $user->firstName, 'last_name' => $user->lastName]);
     } catch (UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Example #12
0
 public function update($userID, $login, $password, $email)
 {
     try {
         $this->database->table(self::TABLE_NAME)->get($userID)->update(array(self::COLUMN_NAME => $login, self::COLUMN_PASSWORD_HASH => Passwords::hash($password), self::COLUMN_EMAIL => $email));
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
 /**
  * @param string|null $password
  */
 public function renderDefault($password = NULL)
 {
     if (!empty($password)) {
         $this->template->hash = \Nette\Security\Passwords::hash($password);
     }
     $this->setLayout(FALSE);
     $this->template->setFile(__DIR__ . '/template.latte');
 }
 /**
  * Adds new user.
  *
  * @param  string
  * @param  string
  */
 public function add($username, $password)
 {
     try {
         $this->connection->query('INSERT INTO [' . table(self::TABLE_NAME) . ']', array(self::COLUMN_NAME => $username, self::COLUMN_PASSWORD_HASH => Passwords::hash($password)));
     } catch (Exception $e) {
         throw new DuplicateNameException();
     }
 }
Example #15
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @return void
  * @throws DuplicateNameException
  */
 public function add($username, $password, $role = 'guest')
 {
     try {
         $this->db->insert(self::TABLE_NAME, [self::COLUMN_NAME => $username, self::COLUMN_PASSWORD_HASH => Passwords::hash($password), self::COLUMN_ROLE => $role])->execute();
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
 /**
  * @param string $username
  * @param string $password
  * @param string $fullname
  */
 public function add($username, $password, $fullname)
 {
     $user = new User();
     $user->setUsername($username);
     $user->setPassword(Passwords::hash($password));
     $user->setFullname($fullname);
     $this->userDao->safePersist($user);
 }
Example #17
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @param  int
  * @return void
  */
 public function add($sUserName, $sPassword, $sEmail, $iRole)
 {
     try {
         $this->oDatabase->table(self::TABLE_NAME)->insert(array(self::COLUMN_NAME => $sUserName, self::COLUMN_PASSWORD_HASH => Passwords::hash($sPassword), self::COLUMN_MAIL => $sEmail, self::COLUMN_TYP => $iRole));
     } catch (Nette\Database\UniqueConstraintViolationException $oException) {
         throw new DuplicateNameException();
     }
 }
 public function onBeforeChange(Form $form, User $user)
 {
     $values = $form->getValues();
     if (!Passwords::verify($values['currentPassword'], $user->password)) {
         $this->flashMessage('Heslo nelze změnit, protože nesouhlasí
              Vaše aktuální heslo.', 'warning');
         $this->redirect('this');
     }
 }
Example #19
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @return void
  */
 public function add($username, $email, $password)
 {
     Passwords::validateNew($password);
     try {
         $this->db->table(static::TABLE_NAME)->insert(array(static::COLUMN_NAME => $username, static::COLUMN_EMAIL => $email, static::COLUMN_PASSWORD_HASH => Security\Passwords::hash($password)));
     } catch (Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Example #20
0
 /**
  * Adds new user.
  * @param  string
  * @param  string
  * @param  string
  * @param  DateTime
  * @return void
  */
 public function add($username, $password, $email, $date)
 {
     try {
         $data = array('username' => $username, 'password' => Passwords::hash($password), 'email' => $email, 'date' => $date, 'role' => 'user');
         $this->usersFacade->update($data);
     } catch (Nette\Database\UniqueConstraintViolationException $e) {
         throw new DuplicateNameException();
     }
 }
Example #21
0
 public function updateUserPasswd($curentPasswd, $newPasswd)
 {
     $row = $this->database->table(self::USER_TABLE_NAME)->where(self::USER_COLUMN_ID, $this->user->identity->id)->fetch();
     if (Passwords::verify($curentPasswd, $row[self::USER_COLUMN_PASSWORD])) {
         $this->database->table(self::USER_TABLE_NAME)->where(self::USER_COLUMN_ID, $this->user->identity->id)->update(array(self::USER_COLUMN_PASSWORD => Passwords::hash($newPasswd)));
         return True;
     } else {
         return False;
     }
 }
Example #22
0
 /**
  * Compare $password to origin
  * @param $password
  * @return bool
  */
 public function verifyPassword($password)
 {
     if (Passwords::verify($password, $this->password)) {
         if (Passwords::needsRehash($this->password)) {
             $this->setPassword($password);
         }
         return TRUE;
     }
     return FALSE;
 }
 /**
  * @deprecated
  * @param string $password
  * @param $user
  * @throws AuthenticationException
  */
 private function authOldPassword($password, $user)
 {
     list($_, $hash, $salt) = explode(';', $user->password);
     if ($this->calculateHash($password, $salt) !== $hash) {
         throw new AuthenticationException('auth.flash.wrongPassword', self::INVALID_CREDENTIAL);
     }
     $plainHash = Passwords::hash($password);
     $user->password = $this->aes->encrypt($plainHash);
     $this->orm->flush();
 }
Example #24
0
 function signUpFormSucceeded(\Nette\Forms\BootstrapUIForm $form)
 {
     $activationCode = \Nette\Utils\Random::generate(12, "987654321zyxwvutsrqponmlkjihgfedcba");
     $password = \Nette\Security\Passwords::hash($form->values->pwd);
     $arr = array("email" => $form->values->email, "username" => $form->values->username, "password" => $password, "activation" => $activationCode, "newsletter" => (bool) $form->values->newsletter, "state" => 0, "users_roles_id" => 4, "date_created" => date("Y-m-d H:i:s"));
     if ($this->presenter->template->settings['members:groups:enabled']) {
         $arr["categories_id"] = $form->values->group;
     }
     $userId = $this->database->table("users")->insert($arr);
     $this->database->table("users")->where(array("id" => $userId->id))->update(array("uid" => \Nette\Utils\Strings::padLeft($userId->id, 6, '0')));
     if ($this->template->settings['members:signup:contactEnabled']) {
         $arrContacts = array("categories_id" => 44, "users_id" => $userId, "name" => $form->values->name, "street" => $form->values->street, "city" => $form->values->city, "zip" => $form->values->zip, "countries_id" => 1);
         if ($this->presenter->template->settings['members:signup:companyEnabled']) {
             $arrContacts["company"] = $form->values->company;
             $arrContacts["vatin"] = $form->values->vatin;
             $arrContacts["vatid"] = $form->values->vatid;
         }
         $contactId = $this->database->table("contacts")->insert($arrContacts);
         $this->database->table("contacts")->get($contactId)->update(array("order" => $contactId));
     }
     if ($form->values->vatin) {
         $ares = new \h4kuna\Ares\Ares();
         $aresArr = $ares->loadData('')->toArray();
     }
     $latte = new \Latte\Engine();
     $latte->setLoader(new \Latte\Loaders\StringLoader());
     $params = array('username' => $form->values->username, 'activationCode' => $activationCode, 'settings' => $this->presenter->template->settings, 'form' => $form, 'aresArr' => $aresArr);
     $helpdesk = $this->database->table("helpdesk")->get(3);
     $helpdesk_signup_member = $helpdesk->related("helpdesk_emails", "helpdesk_id")->get(5);
     $helpdesk_signup_confirmbyadmin = $helpdesk->related("helpdesk_emails", "helpdesk_id")->get(6);
     $helpdesk_signup_adminconfirm = $helpdesk->related("helpdesk_emails", "helpdesk_id")->get(7);
     try {
         if ($this->presenter->template->settings['members:signup:confirmByAdmin']) {
             $email_signup_confirmbyamin = $latte->renderToString($helpdesk_signup_confirmbyadmin->body, $params);
             $email_signup_adminconfirm = $latte->renderToString($helpdesk_signup_adminconfirm->body, $params);
             $mail = new \Nette\Mail\Message();
             $mail->setFrom($this->presenter->template->settings['contacts:email:hq'])->addTo($form->values->email)->setHTMLBody($email_signup_confirmbyamin);
             $this->presenter->mailer->send($mail);
             $mailA = new \Nette\Mail\Message();
             $mailA->setFrom($this->presenter->template->settings['contacts:email:hq'])->addTo($this->presenter->template->settings['contacts:email:hq'])->setHTMLBody($email_signup_adminconfirm);
             $this->presenter->mailer->send($mailA);
             $this->flashMessage('Registrace byla dokončena. Po ověření Vám bude zaslán e-mail, po kterém se můžete přihlásit', 'note');
         } else {
             $email_signup_member = $latte->renderToString($helpdesk_signup_member->body, $params);
             $mail = new \Nette\Mail\Message();
             $mail->setFrom($this->presenter->template->settings['contacts:email:hq'])->addTo($form->values->email)->setHTMLBody($email_signup_member);
             $this->presenter->mailer->send($mail);
             $this->presenter->flashMessage('Vaše registrace proběhla úspěšně. Po ověření se můžete přihlásit.', 'note');
         }
         $this->presenter->redirect(":Front:Sign:ed");
     } catch (\Nette\Mail\SmtpException $e) {
         $this->presenter->flashMessage('E-mail nebyl odeslán' . $e->getMessage(), 'error');
         $this->presenter->redirect(":Front:Sign:up");
     }
 }
Example #25
0
 public function changePassword($oldPassword, $newPassword)
 {
     $this->auth->checkCredentials([$this->user->identity->name, $oldPassword]);
     $user = $this->userService->get((int) $this->user->identity->id);
     $login = $user->login;
     $login->password = Nette\Security\Passwords::hash($newPassword);
     $login->passwordLastChangedAt = new Nette\Utils\DateTime();
     $this->userLoginService->persist($login);
     $this->userLoginService->flush();
     $this->aclFactory->invalidateCache();
 }
Example #26
0
 public function changePassword($userId, $oldPassword, $newPassword)
 {
     $user = $this->get($userId);
     if (Nette\Security\Passwords::verify($oldPassword, $user->passwordHash)) {
         $user->passwordHash = Nette\Security\Passwords::hash($newPassword);
         $this->em->flush();
         return TRUE;
     } else {
         return FALSE;
     }
 }
Example #27
0
 /**
  * Vytvoří nového uživatele a uloží jej do databáze.
  * @param \Nette\Utils\ArrayHash $values
  * @param int $defaultGroupId
  * @param int $status
  * @return \Model\Core\User\User
  */
 public function createNewUser(\Nette\Utils\ArrayHash $values, $defaultGroupId = BasicGroupId::USER, $status = \Model\Common\RecordStatus::VALID)
 {
     $user = new User();
     $user->setUsername($values->username);
     $user->setPassword(\Nette\Security\Passwords::hash($values->password));
     $user->setEmail($values->email);
     $user->setGroupId($defaultGroupId);
     $user->setStatus($status);
     $this->saveUser($user);
     return $user;
 }
Example #28
0
 /**
  * @param array $credentials
  * @return \Nette\Security\Identity
  * @throws \Nette\Security\AuthenticationException
  */
 public function authenticate(array $credentials)
 {
     list($username, $password) = $credentials;
     $user = $this->repository->findUserByName($username);
     if (!is_null($user) && \Nette\Security\Passwords::verify($password, $user->getPassword())) {
         $group = $this->repository->findGroupById($user->getGroupId());
         return new \Nette\Security\Identity($user->id, $group->getName(), $user->getIdentity());
     } else {
         throw new \Nette\Security\AuthenticationException($this->t("forms.sign.errors.wrong-credentials"), self::IDENTITY_NOT_FOUND);
     }
 }
Example #29
0
 public function authenticate($username, $password)
 {
     /** @var \stdClass $user */
     $user = $this->userModel->getByUsername($username);
     if (!$user) {
         throw new Nette\Security\AuthenticationException('Username does not exist.');
     } elseif (!Nette\Security\Passwords::verify($password, $user->password)) {
         throw new Nette\Security\AuthenticationException('The password is incorrect.');
     }
     unset($user->password);
     return new Nette\Security\Identity($user->id, $user->access, $user);
 }
Example #30
0
 /**
  * @param string $email
  * @param string $password
  * @param string $roleStringIdentifier
  * @return User
  * @throws RoleDoesNotExistException
  * @throws UserAlreadyExistsException
  */
 public function register(string $email, string $password, string $roleStringIdentifier)
 {
     $entityManager = $this->registry->getManager();
     $email = UserTools::sanitizeUserEmail($email);
     $this->validateUserDoesNotExists($email);
     $passwordHash = Passwords::hash($password);
     $user = new User($email, $passwordHash);
     $entityManager->persist($user);
     $this->roleToUserAssigner->assignByString($roleStringIdentifier, $user);
     $entityManager->flush($user);
     return $user;
 }