2013-12-23 02:55:53 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-25 03:17:58 +02:00
|
|
|
namespace Miniflux\Model\User;
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2014-02-08 20:13:14 +01:00
|
|
|
use PicoDb\Database;
|
2016-12-26 15:44:53 +01:00
|
|
|
use Miniflux\Helper;
|
2013-12-23 02:55:53 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
const TABLE = 'users';
|
|
|
|
|
|
|
|
function create_user($username, $password, $is_admin = false)
|
2014-11-19 01:00:53 +01:00
|
|
|
{
|
2016-12-29 23:53:46 +01:00
|
|
|
$username = trim($username);
|
|
|
|
$password = trim($password);
|
2016-12-26 15:44:53 +01:00
|
|
|
list($fever_token, $fever_api_key) = generate_fever_api_key($username);
|
|
|
|
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->persist(array(
|
|
|
|
'username' => $username,
|
|
|
|
'password' => password_hash($password, PASSWORD_BCRYPT),
|
|
|
|
'is_admin' => (int) $is_admin,
|
|
|
|
'api_token' => Helper\generate_token(),
|
|
|
|
'bookmarklet_token' => Helper\generate_token(),
|
|
|
|
'cronjob_token' => Helper\generate_token(),
|
|
|
|
'feed_token' => Helper\generate_token(),
|
|
|
|
'fever_token' => $fever_token,
|
|
|
|
'fever_api_key' => $fever_api_key,
|
|
|
|
));
|
2014-11-19 01:00:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function update_user($user_id, $username, $password = null, $is_admin = null)
|
2014-11-19 01:00:53 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
$user = get_user_by_id($user_id);
|
|
|
|
$values = array();
|
|
|
|
|
|
|
|
if ($user['username'] !== $username) {
|
|
|
|
list($fever_token, $fever_api_key) = generate_fever_api_key($user['username']);
|
|
|
|
|
|
|
|
$values['username'] = $username;
|
|
|
|
$values['fever_token'] = $fever_token;
|
|
|
|
$values['fever_api_key'] = $fever_api_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($password !== null) {
|
|
|
|
$values['password'] = password_hash($password, PASSWORD_BCRYPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_admin !== null) {
|
|
|
|
$values['is_admin'] = (int) $is_admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! empty($values)) {
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $user_id)
|
|
|
|
->update($values);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function regenerate_tokens($user_id)
|
|
|
|
{
|
|
|
|
$user = get_user_by_id($user_id);
|
|
|
|
list($fever_token, $fever_api_key) = generate_fever_api_key($user['username']);
|
|
|
|
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $user_id)
|
|
|
|
->update(array(
|
|
|
|
'api_token' => Helper\generate_token(),
|
|
|
|
'bookmarklet_token' => Helper\generate_token(),
|
|
|
|
'cronjob_token' => Helper\generate_token(),
|
|
|
|
'feed_token' => Helper\generate_token(),
|
|
|
|
'fever_token' => $fever_token,
|
|
|
|
'fever_api_key' => $fever_api_key,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_user($user_id)
|
|
|
|
{
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $user_id)
|
|
|
|
->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_fever_api_key($username)
|
|
|
|
{
|
|
|
|
$token = Helper\generate_token();
|
|
|
|
$api_key = md5($username . ':' . $token);
|
|
|
|
return array($token, $api_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_all_users()
|
|
|
|
{
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->columns('id', 'username', 'is_admin', 'last_login')
|
|
|
|
->asc('username')
|
|
|
|
->asc('id')
|
|
|
|
->findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_user_by_id($user_id)
|
|
|
|
{
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $user_id)
|
|
|
|
->findOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_user_by_id_without_password($user_id)
|
|
|
|
{
|
|
|
|
$user = Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $user_id)
|
|
|
|
->findOne();
|
|
|
|
|
|
|
|
unset($user['password']);
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_user_by_username($username)
|
|
|
|
{
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('username', $username)
|
|
|
|
->findOne();
|
2014-11-19 01:00:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_user_by_token($key, $token)
|
2013-12-23 02:55:53 +01:00
|
|
|
{
|
2015-08-15 03:33:39 +02:00
|
|
|
return Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
|
|
|
->eq($key, $token)
|
|
|
|
->findOne();
|
2013-12-23 02:55:53 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function set_last_login_date($user_id)
|
2016-03-15 02:34:50 +01:00
|
|
|
{
|
|
|
|
return Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $user_id)
|
|
|
|
->update(array('last_login' => time()));
|
2016-03-15 02:34:50 +01:00
|
|
|
}
|