Esempio n. 1
0
 /**
  * Display error page if no page was found for the current route.
  *
  * @param Event $event
  */
 public function onPageNotFound(Event $event)
 {
     /** @var Pages $pages */
     $pages = $this->grav['pages'];
     // Try to load user error page.
     $page = $pages->dispatch($this->config->get('plugins.error.routes.404', '/error'), true);
     if (!$page) {
         // If none provided use built in error page.
         $page = new Page();
         $page->init(new \SplFileInfo(__DIR__ . '/pages/error.md'));
     }
     $event->page = $page;
     $event->stopPropagation();
 }
Esempio n. 2
0
 /**
  * Handle form validation error
  *
  * @param  Event $event An event object
  */
 public function onFormValidationError(Event $event)
 {
     $form = $event['form'];
     if (isset($event['message'])) {
         $form->message_color = 'red';
         $form->message = $event['message'];
         $form->messages = $event['messages'];
     }
     $uri = $this->grav['uri'];
     $route = $uri->route();
     /** @var Twig $twig */
     $twig = $this->grav['twig'];
     $twig->twig_vars['form'] = $form;
     /** @var Pages $pages */
     $pages = $this->grav['pages'];
     $page = $pages->dispatch($route, true);
     if ($page) {
         unset($this->grav['page']);
         $this->grav['page'] = $page;
     }
     $event->stopPropagation();
 }
Esempio n. 3
0
 /**
  * Process a registration form. Handles the following actions:
  *
  * - register_user: registers a user
  *
  * @param Event $event
  */
 public function onFormProcessed(Event $event)
 {
     $form = $event['form'];
     $action = $event['action'];
     $params = $event['params'];
     switch ($action) {
         case 'register_user':
             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');
             if (file_exists($this->grav['locator']->findResource('user://accounts/' . $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']);
             }
             // Don't store the username: that is part of the filename
             unset($data['username']);
             if ($this->config->get('plugins.login.user_registration.options.set_user_disabled', false)) {
                 $data['state'] = 'disabled';
             } else {
                 $data['state'] = 'enabled';
             }
             // Create user object and save it
             $user = new User($data);
             $file = CompiledYamlFile::instance($this->grav['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
             $user->file($file);
             $user->save();
             $user = User::load($username);
             if ($data['state'] == 'enabled' && $this->config->get('plugins.login.user_registration.options.login_after_registration', false)) {
                 //Login user
                 $this->grav['session']->user = $user;
                 unset($this->grav['user']);
                 $this->grav['user'] = $user;
                 $user->authenticated = $user->authorize('site.login');
             }
             if ($this->config->get('plugins.login.user_registration.options.send_activation_email', false)) {
                 $this->sendActivationEmail($user);
             } else {
                 if ($this->config->get('plugins.login.user_registration.options.send_welcome_email', false)) {
                     $this->sendWelcomeEmail($user);
                 }
                 if ($this->config->get('plugins.login.user_registration.options.send_notification_email', false)) {
                     $this->sendNotificationEmail($user);
                 }
             }
             if ($redirect = $this->config->get('plugins.login.user_registration.redirect_after_registration', false)) {
                 $this->grav->redirect($redirect);
             }
             break;
     }
 }
Esempio n. 4
0
 /**
  * Process the admin registration form.
  *
  * @param Event $event
  */
 public function onFormProcessed(Event $event)
 {
     $form = $event['form'];
     $action = $event['action'];
     switch ($action) {
         case 'register_admin_user':
             if (!$this->config->get('plugins.login.enabled')) {
                 throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.PLUGIN_LOGIN_DISABLED'));
             }
             $data = [];
             $username = $form->value('username');
             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 = ['email', 'fullname', 'title'];
             foreach ($fields as $field) {
                 // Process value of field if set in the page process.register_user
                 if (!isset($data[$field]) && $form->value($field)) {
                     $data[$field] = $form->value($field);
                 }
             }
             unset($data['password1']);
             unset($data['password2']);
             // Don't store the username: that is part of the filename
             unset($data['username']);
             // Extra lowercase to ensure file is saved lowercase
             $username = strtolower($username);
             $inflector = new Inflector();
             $data['fullname'] = isset($data['fullname']) ? $data['fullname'] : $inflector->titleize($username);
             $data['title'] = isset($data['title']) ? $data['title'] : 'Administrator';
             $data['state'] = 'enabled';
             $data['access'] = ['admin' => ['login' => true, 'super' => true], 'site' => ['login' => true]];
             // Create user object and save it
             $user = new User($data);
             $file = CompiledYamlFile::instance($this->grav['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
             $user->file($file);
             $user->save();
             $user = User::load($username);
             //Login user
             $this->grav['session']->user = $user;
             unset($this->grav['user']);
             $this->grav['user'] = $user;
             $user->authenticated = $user->authorize('site.login');
             $messages = $this->grav['messages'];
             $messages->add($this->grav['language']->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info');
             $this->grav->redirect($this->admin_route);
             break;
     }
 }
Esempio n. 5
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);
     }
 }