public function register() : View
 {
     if ($this->identity->isAuthorised()) {
         $this->redirect('home');
     }
     $service = new AccountService($this->dbContext);
     if ($this->context->getMethod() == 'post') {
         $this->validateToken();
         $model = new RegisterBindingModel();
         if ($model->getErrors()) {
             foreach ($model->getErrors() as $error) {
                 $this->addErrorMessage($error);
             }
             return new View('account', 'register', $model);
         }
         $result = $service->register($model);
         if (isset($result['error'])) {
             $this->addErrorMessage($result['error']);
             return new View('account', 'register', $model);
         }
         $this->addInfoMessage($result['success']);
         return new View('account', 'login');
     }
     return new View('account', 'register');
 }
Пример #2
0
 public function register(RegisterBindingModel $model)
 {
     if (!$model) {
         $this->response['error'] = 'Invalid model!';
         return $this->response;
     }
     $usernameCheck = $this->dbContext->getUsersRepository()->filterByUsername(" = '" . $model->getUsername() . "'")->findOne();
     if ($usernameCheck->getId()) {
         $this->response['error'] = 'Username already taken!';
         return $this->response;
     }
     $emailCheck = $this->dbContext->getUsersRepository()->filterByEmail(" = '" . $model->getEmail() . "'")->findOne();
     if ($emailCheck->getId()) {
         $this->response['error'] = 'Email already in use!';
         return $this->response;
     }
     $user = new User($model->getUsername(), password_hash($model->getPassword(), PASSWORD_BCRYPT), $model->getEmail(), $model->getTelephone());
     $this->dbContext->getUsersRepository()->add($user);
     $this->dbContext->saveChanges();
     $user = $this->dbContext->getUsersRepository()->filterByUsername(" = '" . $model->getUsername() . "'")->findOne();
     $userId = intval($user->getId());
     $userRole = new Usersrole($userId, 2, 0);
     $this->dbContext->getUsersrolesRepository()->add($userRole);
     $this->dbContext->saveChanges();
     $this->response['success'] = 'Register successful!';
     return $this->response;
 }