aboutsummaryrefslogtreecommitdiff
path: root/app/Controllers/userController.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2017-12-16 15:24:13 +0100
committerGravatar GitHub <noreply@github.com> 2017-12-16 15:24:13 +0100
commitfdc9e0d75a786101a14f64bc418b48fdd1cb4890 (patch)
tree9a7a1d523ab1279e2efce84d2d0c73dd0ad47c70 /app/Controllers/userController.php
parentf7560c585f211be41b093906e3a8fb5a6071c660 (diff)
parentccb829418d25af49d129ac227b0cbd09c085b8a3 (diff)
Merge branch 'dev' into hebrew-i18n
Diffstat (limited to 'app/Controllers/userController.php')
-rw-r--r--app/Controllers/userController.php299
1 files changed, 186 insertions, 113 deletions
diff --git a/app/Controllers/userController.php b/app/Controllers/userController.php
index ed01b83c5..2a1d43d9e 100644
--- a/app/Controllers/userController.php
+++ b/app/Controllers/userController.php
@@ -12,63 +12,83 @@ class FreshRSS_user_Controller extends Minz_ActionController {
* This action is called before every other action in that class. It is
* the common boiler plate for every action. It is triggered by the
* underlying framework.
+ *
+ * @todo clean up the access condition.
*/
public function firstAction() {
- if (!FreshRSS_Auth::hasAccess()) {
+ if (!FreshRSS_Auth::hasAccess() && !(
+ Minz_Request::actionName() === 'create' &&
+ !max_registrations_reached()
+ )) {
Minz_Error::error(403);
}
}
+ public static function hashPassword($passwordPlain) {
+ if (!function_exists('password_hash')) {
+ include_once(LIB_PATH . '/password_compat.php');
+ }
+ $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
+ $passwordPlain = '';
+ $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
+ return $passwordHash == '' ? '' : $passwordHash;
+ }
+
/**
- * This action displays the user profile page.
+ * The username is also used as folder name, file name, and part of SQL table name.
+ * '_' is a reserved internal username.
*/
- public function profileAction() {
- Minz_View::prependTitle(_t('conf.profile.title') . ' · ');
+ const USERNAME_PATTERN = '[0-9a-zA-Z_]{2,38}|[0-9a-zA-Z]';
- if (Minz_Request::isPost()) {
- $ok = true;
+ public static function checkUsername($username) {
+ return preg_match('/^' . self::USERNAME_PATTERN . '$/', $username) === 1;
+ }
- $passwordPlain = Minz_Request::param('passwordPlain', '', true);
- if ($passwordPlain != '') {
- Minz_Request::_param('passwordPlain'); //Discard plain-text password ASAP
- $_POST['passwordPlain'] = '';
- if (!function_exists('password_hash')) {
- include_once(LIB_PATH . '/password_compat.php');
- }
- $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
- $passwordPlain = '';
- $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
- $ok &= ($passwordHash != '');
- FreshRSS_Context::$user_conf->passwordHash = $passwordHash;
- }
- Minz_Session::_param('passwordHash', FreshRSS_Context::$user_conf->passwordHash);
+ public static function updateContextUser($passwordPlain, $apiPasswordPlain, $userConfigUpdated = array()) {
+ if ($passwordPlain != '') {
+ $passwordHash = self::hashPassword($passwordPlain);
+ FreshRSS_Context::$user_conf->passwordHash = $passwordHash;
+ }
- $passwordPlain = Minz_Request::param('apiPasswordPlain', '', true);
- if ($passwordPlain != '') {
- if (!function_exists('password_hash')) {
- include_once(LIB_PATH . '/password_compat.php');
+ if ($apiPasswordPlain != '') {
+ $apiPasswordHash = self::hashPassword($apiPasswordPlain);
+ FreshRSS_Context::$user_conf->apiPasswordHash = $apiPasswordHash;
+ }
+
+ if (is_array($userConfigUpdated)) {
+ foreach ($userConfigUpdated as $configName => $configValue) {
+ if ($configValue !== null) {
+ FreshRSS_Context::$user_conf->_param($configName, $configValue);
}
- $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
- $passwordPlain = '';
- $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
- $ok &= ($passwordHash != '');
- FreshRSS_Context::$user_conf->apiPasswordHash = $passwordHash;
}
+ }
- // TODO: why do we need of hasAccess here?
- if (FreshRSS_Auth::hasAccess('admin')) {
- FreshRSS_Context::$user_conf->mail_login = Minz_Request::param('mail_login', '', true);
- }
- $email = FreshRSS_Context::$user_conf->mail_login;
- Minz_Session::_param('mail', $email);
+ $ok = FreshRSS_Context::$user_conf->save();
+ return $ok;
+ }
+
+ /**
+ * This action displays the user profile page.
+ */
+ public function profileAction() {
+ Minz_View::prependTitle(_t('conf.profile.title') . ' · ');
- $ok &= FreshRSS_Context::$user_conf->save();
+ Minz_View::appendScript(Minz_Url::display(
+ '/scripts/bcrypt.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/bcrypt.min.js')
+ ));
- if ($email != '') {
- $personaFile = DATA_PATH . '/persona/' . $email . '.txt';
- @unlink($personaFile);
- $ok &= (file_put_contents($personaFile, Minz_Session::param('currentUser', '_')) !== false);
- }
+ if (Minz_Request::isPost()) {
+ $passwordPlain = Minz_Request::param('newPasswordPlain', '', true);
+ Minz_Request::_param('newPasswordPlain'); //Discard plain-text password ASAP
+ $_POST['newPasswordPlain'] = '';
+
+ $apiPasswordPlain = Minz_Request::param('apiPasswordPlain', '', true);
+
+ $ok = self::updateContextUser($passwordPlain, $apiPasswordPlain, array(
+ 'token' => Minz_Request::param('token', null),
+ ));
+
+ Minz_Session::_param('passwordHash', FreshRSS_Context::$user_conf->passwordHash);
if ($ok) {
Minz_Request::good(_t('feedback.profile.updated'),
@@ -100,72 +120,82 @@ class FreshRSS_user_Controller extends Minz_ActionController {
// Get information about the current user.
$entryDAO = FreshRSS_Factory::createEntryDao($this->view->current_user);
$this->view->nb_articles = $entryDAO->count();
- $this->view->size_user = $entryDAO->size();
+
+ $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
+ $this->view->size_user = $databaseDAO->size();
}
- public function createAction() {
- if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
- $db = FreshRSS_Context::$system_conf->db;
- require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
+ public static function createUser($new_user_name, $passwordPlain, $apiPasswordPlain, $userConfig = array(), $insertDefaultFeeds = true) {
+ if (!is_array($userConfig)) {
+ $userConfig = array();
+ }
- $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$user_conf->language);
+ $ok = self::checkUsername($new_user_name);
+ $homeDir = join_path(DATA_PATH, 'users', $new_user_name);
+
+ if ($ok) {
$languages = Minz_Translate::availableLanguages();
- if (!isset($languages[$new_user_language])) {
- $new_user_language = FreshRSS_Context::$user_conf->language;
+ if (empty($userConfig['language']) || !in_array($userConfig['language'], $languages)) {
+ $userConfig['language'] = 'en';
}
- $new_user_name = Minz_Request::param('new_user_name');
- $ok = ($new_user_name != '') && ctype_alnum($new_user_name);
-
- if ($ok) {
- $default_user = FreshRSS_Context::$system_conf->default_user;
- $ok &= (strcasecmp($new_user_name, $default_user) !== 0); //It is forbidden to alter the default user
-
- $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
+ $ok &= !in_array(strtoupper($new_user_name), array_map('strtoupper', listUsers())); //Not an existing user, case-insensitive
- $configPath = join_path(DATA_PATH, 'users', $new_user_name, 'config.php');
- $ok &= !file_exists($configPath);
+ $configPath = join_path($homeDir, 'config.php');
+ $ok &= !file_exists($configPath);
+ }
+ if ($ok) {
+ $passwordHash = '';
+ if ($passwordPlain != '') {
+ $passwordHash = self::hashPassword($passwordPlain);
+ $ok &= ($passwordHash != '');
}
- if ($ok) {
- $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
- $passwordHash = '';
- if ($passwordPlain != '') {
- Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
- $_POST['new_user_passwordPlain'] = '';
- if (!function_exists('password_hash')) {
- include_once(LIB_PATH . '/password_compat.php');
- }
- $passwordHash = password_hash($passwordPlain, PASSWORD_BCRYPT, array('cost' => self::BCRYPT_COST));
- $passwordPlain = '';
- $passwordHash = preg_replace('/^\$2[xy]\$/', '\$2a\$', $passwordHash); //Compatibility with bcrypt.js
- $ok &= ($passwordHash != '');
- }
- if (empty($passwordHash)) {
- $passwordHash = '';
- }
- $new_user_email = filter_var($_POST['new_user_email'], FILTER_VALIDATE_EMAIL);
- if (empty($new_user_email)) {
- $new_user_email = '';
- } else {
- $personaFile = join_path(DATA_PATH, 'persona', $new_user_email . '.txt');
- @unlink($personaFile);
- $ok &= (file_put_contents($personaFile, $new_user_name) !== false);
- }
+ $apiPasswordHash = '';
+ if ($apiPasswordPlain != '') {
+ $apiPasswordHash = self::hashPassword($apiPasswordPlain);
+ $ok &= ($apiPasswordHash != '');
}
- if ($ok) {
- mkdir(join_path(DATA_PATH, 'users', $new_user_name));
- $config_array = array(
- 'language' => $new_user_language,
- 'passwordHash' => $passwordHash,
- 'mail_login' => $new_user_email,
- );
- $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($config_array, true) . ';') !== false);
- }
- if ($ok) {
- $userDAO = new FreshRSS_UserDAO();
- $ok &= $userDAO->createUser($new_user_name);
+ }
+ if ($ok) {
+ if (!is_dir($homeDir)) {
+ mkdir($homeDir);
}
+ $userConfig['passwordHash'] = $passwordHash;
+ $userConfig['apiPasswordHash'] = $apiPasswordHash;
+ $ok &= (file_put_contents($configPath, "<?php\n return " . var_export($userConfig, true) . ';') !== false);
+ }
+ if ($ok) {
+ $userDAO = new FreshRSS_UserDAO();
+ $ok &= $userDAO->createUser($new_user_name, $userConfig['language'], $insertDefaultFeeds);
+ }
+ return $ok;
+ }
+
+ /**
+ * This action creates a new user.
+ *
+ * Request parameters are:
+ * - new_user_language
+ * - new_user_name
+ * - new_user_passwordPlain
+ * - r (i.e. a redirection url, optional)
+ *
+ * @todo clean up this method. Idea: write a method to init a user with basic information.
+ * @todo handle r redirection in Minz_Request::forward directly?
+ */
+ public function createAction() {
+ if (Minz_Request::isPost() && (
+ FreshRSS_Auth::hasAccess('admin') ||
+ !max_registrations_reached()
+ )) {
+ $new_user_name = Minz_Request::param('new_user_name');
+ $passwordPlain = Minz_Request::param('new_user_passwordPlain', '', true);
+ $new_user_language = Minz_Request::param('new_user_language', FreshRSS_Context::$user_conf->language);
+
+ $ok = self::createUser($new_user_name, $passwordPlain, '', array('language' => $new_user_language));
+ Minz_Request::_param('new_user_passwordPlain'); //Discard plain-text password ASAP
+ $_POST['new_user_passwordPlain'] = '';
invalidateHttpCache();
$notif = array(
@@ -175,30 +205,73 @@ class FreshRSS_user_Controller extends Minz_ActionController {
Minz_Session::_param('notification', $notif);
}
- Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
+ $redirect_url = urldecode(Minz_Request::param('r', false, true));
+ if (!$redirect_url) {
+ $redirect_url = array('c' => 'user', 'a' => 'manage');
+ }
+ Minz_Request::forward($redirect_url, true);
+ }
+
+ public static function deleteUser($username) {
+ $db = FreshRSS_Context::$system_conf->db;
+ require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
+
+ $ok = self::checkUsername($username);
+ if ($ok) {
+ $default_user = FreshRSS_Context::$system_conf->default_user;
+ $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
+ }
+ $user_data = join_path(DATA_PATH, 'users', $username);
+ if ($ok) {
+ $ok &= is_dir($user_data);
+ }
+ if ($ok) {
+ $userDAO = new FreshRSS_UserDAO();
+ $ok &= $userDAO->deleteUser($username);
+ $ok &= recursive_unlink($user_data);
+ array_map('unlink', glob(PSHB_PATH . '/feeds/*/' . $username . '.txt'));
+ }
+ return $ok;
}
+ /**
+ * This action delete an existing user.
+ *
+ * Request parameter is:
+ * - username
+ *
+ * @todo clean up this method. Idea: create a User->clean() method.
+ */
public function deleteAction() {
- if (Minz_Request::isPost() && FreshRSS_Auth::hasAccess('admin')) {
- $db = FreshRSS_Context::$system_conf->db;
- require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
+ $username = Minz_Request::param('username');
+ $redirect_url = urldecode(Minz_Request::param('r', false, true));
+ if (!$redirect_url) {
+ $redirect_url = array('c' => 'user', 'a' => 'manage');
+ }
- $username = Minz_Request::param('username');
- $ok = ctype_alnum($username);
- $user_data = join_path(DATA_PATH, 'users', $username);
+ $self_deletion = Minz_Session::param('currentUser', '_') === $username;
- if ($ok) {
- $default_user = FreshRSS_Context::$system_conf->default_user;
- $ok &= (strcasecmp($username, $default_user) !== 0); //It is forbidden to delete the default user
+ if (Minz_Request::isPost() && (
+ FreshRSS_Auth::hasAccess('admin') ||
+ $self_deletion
+ )) {
+ $ok = true;
+ if ($ok && $self_deletion) {
+ // We check the password if it's a self-destruction
+ $nonce = Minz_Session::param('nonce');
+ $challenge = Minz_Request::param('challenge', '');
+
+ $ok &= FreshRSS_FormAuth::checkCredentials(
+ $username, FreshRSS_Context::$user_conf->passwordHash,
+ $nonce, $challenge
+ );
}
if ($ok) {
- $ok &= is_dir($user_data);
+ $ok &= self::deleteUser($username);
}
- if ($ok) {
- $userDAO = new FreshRSS_UserDAO();
- $ok &= $userDAO->deleteUser($username);
- $ok &= recursive_unlink($user_data);
- //TODO: delete Persona file
+ if ($ok && $self_deletion) {
+ FreshRSS_Auth::removeAccess();
+ $redirect_url = array('c' => 'index', 'a' => 'index');
}
invalidateHttpCache();
@@ -209,6 +282,6 @@ class FreshRSS_user_Controller extends Minz_ActionController {
Minz_Session::_param('notification', $notif);
}
- Minz_Request::forward(array('c' => 'user', 'a' => 'manage'), true);
+ Minz_Request::forward($redirect_url, true);
}
}