Example #1
0
 /**
  * Create admin user for Yunohost install
  */
 protected function createUserFromYnh()
 {
     $auth = HttpbasicauthPlugin::extractFromHeaders();
     $username = $auth['username'];
     $user = new User(['password' => $auth['password'], 'email' => !empty($_SERVER['HTTP_EMAIL']) ? $_SERVER['HTTP_EMAIL'] : '', 'fullname' => !empty($_SERVER['HTTP_NAME']) ? $_SERVER['HTTP_NAME'] : '', 'title' => 'Administrator', 'state' => 'enabled', 'access' => ['admin' => ['login' => true, 'super' => true], 'site' => ['login' => true]]]);
     $file = CompiledYamlFile::instance($this->grav['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     return $username;
 }
Example #2
0
 /**
  * Load user account.
  *
  * Always creates user object. To check if user exists, use $this->exists().
  *
  * @param string $username
  * @return User
  */
 public static function load($username)
 {
     // FIXME: validate directory name
     $blueprints = new Blueprints('blueprints://user');
     $blueprint = $blueprints->get('account');
     $file = CompiledYamlFile::instance(ACCOUNTS_DIR . $username . YAML_EXT);
     $content = $file->content();
     if (!isset($content['username'])) {
         $content['username'] = $username;
     }
     $user = new User($content, $blueprint);
     $user->file($file);
     return $user;
 }
Example #3
0
 /**
  * Load user account.
  *
  * Always creates user object. To check if user exists, use $this->exists().
  *
  * @param string $username
  * @return User
  */
 public static function load($username)
 {
     $locator = self::getGrav()['locator'];
     $blueprints = new Blueprints('blueprints://');
     $blueprint = $blueprints->get('user/account');
     $file_path = $locator->findResource('account://' . $username . YAML_EXT);
     $file = CompiledYamlFile::instance($file_path);
     $content = $file->content();
     if (!isset($content['username'])) {
         $content['username'] = $username;
     }
     $user = new User($content, $blueprint);
     $user->file($file);
     return $user;
 }
 /**
  * @return int|null|void
  */
 protected function serve()
 {
     $this->options = ['user' => $this->input->getOption('user'), 'password1' => $this->input->getOption('password')];
     $this->validateOptions();
     $helper = $this->getHelper('question');
     $data = [];
     $this->output->writeln('<green>Changing User Password</green>');
     $this->output->writeln('');
     if (!$this->options['user']) {
         // Get username and validate
         $question = new Question('Enter a <yellow>username</yellow>: ');
         $question->setValidator(function ($value) {
             return $this->validate('user', $value);
         });
         $username = $helper->ask($this->input, $this->output, $question);
     } else {
         $username = $this->options['user'];
     }
     if (!$this->options['password1']) {
         // Get password and validate
         $password = $this->askForPassword($helper, 'Enter a <yellow>new password</yellow>: ', function ($password1) use($helper) {
             $this->validate('password1', $password1);
             // Since input is hidden when prompting for passwords, the user is asked to repeat the password
             return $this->askForPassword($helper, 'Repeat the <yellow>password</yellow>: ', function ($password2) use($password1) {
                 return $this->validate('password2', $password2, $password1);
             });
         });
         $data['password'] = $password;
     } else {
         $data['password'] = $this->options['password1'];
     }
     // Lowercase the username for the filename
     $username = strtolower($username);
     // Grab the account file and read in the information before setting the file (prevent setting erase)
     $oldUserFile = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('account://' . $username . YAML_EXT, true, true));
     $oldData = $oldUserFile->content();
     //Set the password feild to new password
     $oldData['password'] = $data['password'];
     // Create user object and save it using oldData (with updated password)
     $user = new User($oldData);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('account://' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     $this->output->writeln('');
     $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '\'s</cyan> password changed.');
 }
 /**
  * @return int|null|void
  */
 protected function serve()
 {
     $this->options = ['user' => $this->input->getOption('user'), 'state' => $this->input->getOption('state')];
     $this->validateOptions();
     $helper = $this->getHelper('question');
     $data = [];
     $this->output->writeln('<green>Setting User State</green>');
     $this->output->writeln('');
     if (!$this->options['user']) {
         // Get username and validate
         $question = new Question('Enter a <yellow>username</yellow>: ');
         $question->setValidator(function ($value) {
             return $this->validate('user', $value);
         });
         $username = $helper->ask($this->input, $this->output, $question);
     } else {
         $username = $this->options['user'];
     }
     if (!$this->options['state'] && !count(array_filter($this->options))) {
         // Choose State
         $question = new ChoiceQuestion('Please choose the <yellow>state</yellow> for the account:', array('enabled' => 'Enabled', 'disabled' => 'Disabled'), 'enabled');
         $question->setErrorMessage('State %s is invalid.');
         $data['state'] = $helper->ask($this->input, $this->output, $question);
     } else {
         $data['state'] = $this->options['state'] ?: 'enabled';
     }
     // Lowercase the username for the filename
     $username = strtolower($username);
     // Grab the account file and read in the information before setting the file (prevent setting erase)
     $oldUserFile = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $oldData = $oldUserFile->content();
     //Set the state feild to new state
     $oldData['state'] = $data['state'];
     // Create user object and save it using oldData (with updated state)
     $user = new User($oldData);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     $this->output->writeln('');
     $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '</cyan> state set to .' . $data['state']);
 }
Example #6
0
 /**
  * Load user account.
  *
  * Always creates user object. To check if user exists, use $this->exists().
  *
  * @param string $username
  *
  * @return User
  */
 public static function load($username)
 {
     $grav = Grav::instance();
     $locator = $grav['locator'];
     $config = $grav['config'];
     // force lowercase of username
     $username = strtolower($username);
     $blueprints = new Blueprints();
     $blueprint = $blueprints->get('user/account');
     $file_path = $locator->findResource('account://' . $username . YAML_EXT);
     $file = CompiledYamlFile::instance($file_path);
     $content = $file->content();
     if (!isset($content['username'])) {
         $content['username'] = $username;
     }
     if (!isset($content['state'])) {
         $content['state'] = 'enabled';
     }
     $user = new User($content, $blueprint);
     $user->file($file);
     // add user to config
     $config->set("user", $user);
     return $user;
 }
Example #7
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;
     }
 }
Example #8
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;
     }
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->setupConsole($input, $output);
     $helper = $this->getHelper('question');
     $data = [];
     $this->output->writeln('<green>Create new user</green>');
     $this->output->writeln('');
     // Get username and validate
     $question = new Question('Enter a <yellow>username</yellow>: ', 'admin');
     $question->setValidator(function ($value) {
         if (!preg_match('/^[a-z0-9_-]{3,16}$/', $value)) {
             throw new RuntimeException('Username should be between 3 and 16 comprised of lowercase letters, numbers, underscores and hyphens');
         }
         if (file_exists(self::getGrav()['locator']->findResource('user://accounts/' . $value . YAML_EXT))) {
             throw new RuntimeException('Username "' . $value . '" already exists, please pick another username');
         }
         return $value;
     });
     $username = $helper->ask($this->input, $this->output, $question);
     // Get password and validate
     $question = new Question('Enter a <yellow>password</yellow>: ');
     $question->setValidator(function ($value) {
         if (!preg_match('/(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/', $value)) {
             throw new RuntimeException('Password must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters');
         }
         return $value;
     });
     $data['password'] = $helper->ask($this->input, $this->output, $question);
     // Get email and validate
     $question = new Question('Enter an <yellow>email</yellow>:   ');
     $question->setValidator(function ($value) {
         if (!preg_match('/^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$/', $value)) {
             throw new RuntimeException('Not a valid email address');
         }
         return $value;
     });
     $data['email'] = $helper->ask($this->input, $this->output, $question);
     // Choose permissions
     $question = new ChoiceQuestion('Please choose a set of <yellow>permissions</yellow>:', array('a' => 'admin access', 's' => 'site access', 'b' => 'admin and site access'), 'a');
     $question->setErrorMessage('permissions %s is invalid.');
     $permissions_choice = $helper->ask($this->input, $this->output, $question);
     switch ($permissions_choice) {
         case 'a':
             $data['access']['admin'] = ['login' => true, 'super' => true];
             break;
         case 's':
             $data['access']['site'] = ['login' => true];
             break;
         case 'b':
             $data['access']['admin'] = ['login' => true, 'super' => true];
             $data['access']['site'] = ['login' => true];
     }
     // Get fullname
     $question = new Question('Enter a <yellow>fullname</yellow>: ');
     $question->setValidator(function ($value) {
         if ($value === null or trim($value) == '') {
             throw new RuntimeException('Fullname is required');
         }
         return $value;
     });
     $data['fullname'] = $helper->ask($this->input, $this->output, $question);
     // Get title
     $question = new Question('Enter a <yellow>title</yellow>:    ');
     $data['title'] = $helper->ask($this->input, $this->output, $question);
     // Create user object and save it
     $user = new User($data);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     $this->output->writeln('');
     $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '</cyan> created.');
 }
Example #10
0
 /**
  * Process a registration form. Handles the following actions:
  *
  * - validate_password: validates a password
  * - register_user: registers a user
  *
  * @param Event $event
  */
 public function onFormProcessed(Event $event)
 {
     $form = $event['form'];
     $action = $event['action'];
     $params = $event['params'];
     if (!$this->config->get('plugins.login.enabled')) {
         throw new \RuntimeException($this->grav['language']->translate('LOGIN_PLUGIN.LOGIN_PLUGIN_DISABLED'));
     }
     if (!$this->config->get('plugins.login.user_registration.enabled')) {
         throw new \RuntimeException($this->grav['language']->translate('LOGIN_PLUGIN.USER_REGISTRATION_DISABLED'));
     }
     switch ($action) {
         case 'register_user':
             $data = [];
             $username = $form->value('username');
             $this->validate('user', $username);
             if (isset($params['options']['validate_password1_and_password2']) && $params['options']['validate_password1_and_password2']) {
                 $this->validate('password1', $form->value('password1'));
                 $this->validate('password2', $form->value('password2'), $form->value('password1'));
                 $data['password'] = $form->value('password1');
             }
             if (isset($params['options']['validate_password']) && $params['options']['validate_password']) {
                 $this->validate('password1', $form->value('password'));
             }
             $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
                 if (isset($params['fields'])) {
                     foreach ($params['fields'] as $key => $param) {
                         if ($key == $field) {
                             $data[$field] = $param;
                         }
                     }
                 }
                 if (!isset($data[$field]) && $form->value($field)) {
                     $data[$field] = $form->value($field);
                 }
             }
             if (isset($params['options']['validate_password1_and_password2']) && $params['options']['validate_password1_and_password2']) {
                 unset($data['password1']);
                 unset($data['password2']);
             }
             // Don't store the username: that is part of the filename
             unset($data['username']);
             // 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();
             if (isset($params['options']['login_after_registration']) && $params['options']['login_after_registration']) {
                 //Login user
                 $user = User::load($username);
                 $this->grav['session']->user = $user;
                 unset($this->grav['user']);
                 $this->grav['user'] = $user;
                 $user->authenticated = $user->authorize('site.login');
             }
             break;
     }
 }
Example #11
0
 /**
  * @return int|null|void
  */
 protected function serve()
 {
     $this->options = ['user' => $this->input->getOption('user'), 'password1' => $this->input->getOption('password'), 'email' => $this->input->getOption('email'), 'permissions' => $this->input->getOption('permissions'), 'fullname' => $this->input->getOption('fullname'), 'title' => $this->input->getOption('title'), 'state' => $this->input->getOption('state')];
     $this->validateOptions();
     $helper = $this->getHelper('question');
     $data = [];
     $this->output->writeln('<green>Creating new user</green>');
     $this->output->writeln('');
     if (!$this->options['user']) {
         // Get username and validate
         $question = new Question('Enter a <yellow>username</yellow>: ', 'admin');
         $question->setValidator(function ($value) {
             return $this->validate('user', $value);
         });
         $username = $helper->ask($this->input, $this->output, $question);
     } else {
         $username = $this->options['user'];
     }
     if (!$this->options['password1']) {
         // Get password and validate
         $password = $this->askForPassword($helper, 'Enter a <yellow>password</yellow>: ', function ($password1) use($helper) {
             $this->validate('password1', $password1);
             // Since input is hidden when prompting for passwords, the user is asked to repeat the password
             return $this->askForPassword($helper, 'Repeat the <yellow>password</yellow>: ', function ($password2) use($password1) {
                 return $this->validate('password2', $password2, $password1);
             });
         });
         $data['password'] = $password;
     } else {
         $data['password'] = $this->options['password1'];
     }
     if (!$this->options['email']) {
         // Get email and validate
         $question = new Question('Enter an <yellow>email</yellow>:   ');
         $question->setValidator(function ($value) {
             return $this->validate('email', $value);
         });
         $data['email'] = $helper->ask($this->input, $this->output, $question);
     } else {
         $data['email'] = $this->options['email'];
     }
     if (!$this->options['permissions']) {
         // Choose permissions
         $question = new ChoiceQuestion('Please choose a set of <yellow>permissions</yellow>:', array('a' => 'Admin Access', 's' => 'Site Access', 'b' => 'Admin and Site Access'), 'a');
         $question->setErrorMessage('Permissions %s is invalid.');
         $permissions_choice = $helper->ask($this->input, $this->output, $question);
     } else {
         $permissions_choice = $this->options['permissions'];
     }
     switch ($permissions_choice) {
         case 'a':
             $data['access']['admin'] = ['login' => true, 'super' => true];
             break;
         case 's':
             $data['access']['site'] = ['login' => true];
             break;
         case 'b':
             $data['access']['admin'] = ['login' => true, 'super' => true];
             $data['access']['site'] = ['login' => true];
     }
     if (!$this->options['fullname']) {
         // Get fullname
         $question = new Question('Enter a <yellow>fullname</yellow>: ');
         $question->setValidator(function ($value) {
             return $this->validate('fullname', $value);
         });
         $data['fullname'] = $helper->ask($this->input, $this->output, $question);
     } else {
         $data['fullname'] = $this->options['fullname'];
     }
     if (!$this->options['title'] && !count(array_filter($this->options))) {
         // Get title
         $question = new Question('Enter a <yellow>title</yellow>:    ');
         $data['title'] = $helper->ask($this->input, $this->output, $question);
     } else {
         $data['title'] = $this->options['title'];
     }
     if (!$this->options['state'] && !count(array_filter($this->options))) {
         // Choose State
         $question = new ChoiceQuestion('Please choose the <yellow>state</yellow> for the account:', array('enabled' => 'Enabled', 'disabled' => 'Disabled'), 'enabled');
         $question->setErrorMessage('State %s is invalid.');
         $data['state'] = $helper->ask($this->input, $this->output, $question);
     } else {
         $data['state'] = $this->options['state'] ?: 'enabled';
     }
     // Create user object and save it
     $user = new User($data);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     $this->output->writeln('');
     $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '</cyan> created.');
 }
Example #12
0
 /**
  * Create a new user file
  *
  * @param array $data
  *
  * @return User
  */
 public function register($data)
 {
     //Add new user ACL settings
     $groups = $this->config->get('plugins.login.user_registration.groups', []);
     if (count($groups) > 0) {
         $data['groups'] = $groups;
     }
     $access = $this->config->get('plugins.login.user_registration.access.site', []);
     if (count($access) > 0) {
         $data['access']['site'] = $access;
     }
     $username = $data['username'];
     $file = CompiledYamlFile::instance($this->grav['locator']->findResource('account://' . $username . YAML_EXT, true, true));
     // Create user object and save it
     $user = new User($data);
     $user->file($file);
     $user->save();
     if (isset($data['state']) && $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');
     }
     return $user;
 }