Exemplo n.º 1
0
 /**
  * Handle login.
  *
  * @return bool True if the action was performed.
  */
 public function taskLogin()
 {
     /** @var Language $t */
     $t = $this->grav['language'];
     if ($this->authenticate($this->post)) {
         $this->login->setMessage($t->translate('PLUGIN_LOGIN.LOGIN_SUCCESSFUL'));
         $redirect = $this->grav['config']->get('plugins.login.redirect_after_login');
         if (!$redirect) {
             $redirect = $this->grav['uri']->referrer('/');
         }
         $this->setRedirect($redirect);
     } else {
         $user = $this->grav['user'];
         if ($user->username) {
             $this->setMessage($t->translate('PLUGIN_LOGIN.ACCESS_DENIED'), 'error');
         } else {
             $this->setMessage($t->translate('PLUGIN_LOGIN.LOGIN_FAILED'), 'error');
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * Process the user registration, triggered by a registration form
  *
  * @param Form $form
  */
 private function processUserRegistration($form, Event $event)
 {
     if (!$this->config->get('plugins.login.enabled')) {
         throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.PLUGIN_LOGIN_DISABLED'));
     }
     if (!$this->config->get('plugins.login.user_registration.enabled')) {
         throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.USER_REGISTRATION_DISABLED'));
     }
     $data = [];
     $username = $form->value('username');
     $data['username'] = $username;
     if (file_exists($this->grav['locator']->findResource('account://' . $username . YAML_EXT))) {
         $this->grav->fireEvent('onFormValidationError', new Event(['form' => $form, 'message' => $this->grav['language']->translate(['PLUGIN_LOGIN.USERNAME_NOT_AVAILABLE', $username])]));
         $event->stopPropagation();
         return;
     }
     if ($this->config->get('plugins.login.user_registration.options.validate_password1_and_password2', false)) {
         if ($form->value('password1') != $form->value('password2')) {
             $this->grav->fireEvent('onFormValidationError', new Event(['form' => $form, 'message' => $this->grav['language']->translate('PLUGIN_LOGIN.PASSWORDS_DO_NOT_MATCH')]));
             $event->stopPropagation();
             return;
         }
         $data['password'] = $form->value('password1');
     }
     $fields = $this->config->get('plugins.login.user_registration.fields', []);
     foreach ($fields as $field) {
         // Process value of field if set in the page process.register_user
         $default_values = $this->config->get('plugins.login.user_registration.default_values');
         if ($default_values) {
             foreach ($default_values as $key => $param) {
                 $values = explode(',', $param);
                 if ($key == $field) {
                     $data[$field] = $values;
                 }
             }
         }
         if (!isset($data[$field]) && $form->value($field)) {
             $data[$field] = $form->value($field);
         }
     }
     if ($this->config->get('plugins.login.user_registration.options.validate_password1_and_password2', false)) {
         unset($data['password1']);
         unset($data['password2']);
     }
     if ($this->config->get('plugins.login.user_registration.options.set_user_disabled', false)) {
         $data['state'] = 'disabled';
     } else {
         $data['state'] = 'enabled';
     }
     $user = $this->login->register($data);
     if ($this->config->get('plugins.login.user_registration.options.send_activation_email', false)) {
         $this->login->sendActivationEmail($user);
     } else {
         if ($this->config->get('plugins.login.user_registration.options.send_welcome_email', false)) {
             $this->login->sendWelcomeEmail($user);
         }
         if ($this->config->get('plugins.login.user_registration.options.send_notification_email', false)) {
             $this->login->sendNotificationEmail($user);
         }
     }
     $redirect = $this->config->get('plugins.login.user_registration.redirect_after_registration', false);
     if ($redirect) {
         $this->grav->redirect($redirect);
     }
 }