Beispiel #1
0
 public function login(Request $request)
 {
     if ($request->isMethod('POST')) {
         if (!NCModuleCore::verify_captcha($request->get('captcha'))) {
             return static::json_response(['error' => 'failed']);
         }
         /** @var Auth $service */
         $service = NCService::load('User.Auth');
         $user = $service->authenticate($request->get('username'), $request->get('password'));
         if ($user && $user->can('access')) {
             $service->login($user);
             return static::json_response(['status' => 'ok']);
         } else {
             return static::json_response(['error' => 'failed']);
         }
     }
     return $this->view->render('users/login.twig', ['title' => $this->lang->translate('user.auth.title')]);
 }
Beispiel #2
0
 /**
  * User registration page
  */
 public function registration(Request $request, $matches)
 {
     $this->guest_only();
     $data = [];
     if ($request->isMethod('post')) {
         $errors = [];
         $captcha = $request->get('code');
         $data = ['username' => $request->get('username'), 'password' => $request->get('password'), 'email' => $request->get('email'), 'group_id' => $this->settings->get('users_group', \Group::first()->id)];
         // Create user instance
         $user = new User($data);
         // Check captcha
         if (!NCModuleCore::verify_captcha($captcha)) {
             $errors[] = $this->lang->translate('user.auth.code_wrong');
         }
         // Validate password
         if (strlen($user->password) < 6) {
             $errors[] = $this->lang->translate('user.edit.short_password');
         }
         // Validate username
         if (strlen($user->username) < 4) {
             $errors[] = $this->lang->translate('user.edit.short_username');
         }
         if (User::count(['conditions' => ['username = ?', $user->username]]) > 0) {
             $errors[] = $this->lang->translate('user.edit.exists', $user->username);
         }
         // Validate email
         if (strlen($user->email) < 5 || strpos($user->email, '@') < 1) {
             $errors[] = $this->lang->translate('user.edit.wrong_email', $user->email);
         } elseif (User::count(['conditions' => ['email = ?', $user->email]]) > 0) {
             $errors[] = $this->lang->translate('user.edit.exists_email', $user->email);
         }
         if ($errors) {
             $this->view->assign('errors', $errors);
         } else {
             if (!$user->save(true)) {
                 $this->view->twig->addGlobal('errors', [$this->lang->translate('form.failed')]);
             } else {
                 $autenticated = $this->auth->authenticate($data['username'], $data['password']);
                 if ($autenticated) {
                     $this->auth->login($autenticated);
                     return static::redirect_response('/');
                 }
             }
         }
     }
     return $this->view->twig->render('user/registration.twig', ['title' => $this->lang->translate('user.registration.title'), 'data' => $data]);
 }