2015-11-07 03:44:50 +01:00
|
|
|
<?php
|
|
|
|
namespace App\Helpers;
|
2016-03-23 02:54:59 +01:00
|
|
|
|
2015-11-07 03:44:50 +01:00
|
|
|
use App\Models\User;
|
2016-03-23 02:54:59 +01:00
|
|
|
use App\Helpers\CryptoHelper;
|
2015-11-07 03:44:50 +01:00
|
|
|
use Hash;
|
|
|
|
|
|
|
|
class UserHelper {
|
2016-12-09 21:46:38 +01:00
|
|
|
public static $USER_ROLES = [
|
2016-12-02 23:07:59 +01:00
|
|
|
'admin' => 'admin',
|
|
|
|
'default' => '',
|
|
|
|
];
|
|
|
|
|
2015-11-07 03:44:50 +01:00
|
|
|
public static function userExists($username) {
|
2016-03-23 02:54:59 +01:00
|
|
|
/* XXX: used primarily with test cases */
|
|
|
|
|
|
|
|
$user = self::getUserByUsername($username, $inactive=true);
|
2015-11-07 03:44:50 +01:00
|
|
|
|
|
|
|
return ($user ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function emailExists($email) {
|
2016-03-23 02:54:59 +01:00
|
|
|
/* XXX: used primarily with test cases */
|
|
|
|
|
|
|
|
$user = self::getUserByEmail($email, $inactive=true);
|
2015-11-07 03:44:50 +01:00
|
|
|
|
|
|
|
return ($user ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateUsername($username) {
|
|
|
|
return ctype_alnum($username);
|
|
|
|
}
|
|
|
|
|
2017-03-12 23:35:19 +01:00
|
|
|
public static function userIsAdmin($username) {
|
|
|
|
return (self::getUserByUsername($username)->role == self::$USER_ROLES['admin']);
|
2015-11-07 03:44:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function checkCredentials($username, $password) {
|
|
|
|
$user = User::where('active', 1)
|
|
|
|
->where('username', $username)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($user == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-09 00:13:35 +01:00
|
|
|
|
|
|
|
$correct_password = Hash::check($password, $user->password);
|
|
|
|
|
|
|
|
if (!$correct_password) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-07 03:44:50 +01:00
|
|
|
else {
|
2015-11-09 00:13:35 +01:00
|
|
|
return ['username' => $username, 'role' => $user->role];
|
2015-11-07 03:44:50 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-09 01:49:45 +01:00
|
|
|
|
2016-03-23 02:54:59 +01:00
|
|
|
public static function resetRecoveryKey($username) {
|
2015-11-09 01:49:45 +01:00
|
|
|
$recovery_key = CryptoHelper::generateRandomHex(50);
|
2016-03-23 02:54:59 +01:00
|
|
|
$user = self::getUserByUsername($username);
|
2015-12-19 01:10:05 +01:00
|
|
|
|
2016-03-23 02:54:59 +01:00
|
|
|
if (!$user) {
|
2015-11-09 01:49:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->recovery_key = $recovery_key;
|
|
|
|
$user->save();
|
2016-03-23 02:54:59 +01:00
|
|
|
|
2016-05-08 05:01:44 +02:00
|
|
|
return $recovery_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function userResetKeyCorrect($username, $recovery_key, $inactive=false) {
|
|
|
|
// Given a username and a recovery key, return true if they match.
|
|
|
|
$user = self::getUserByUsername($username, $inactive);
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
if ($recovery_key != $user->recovery_key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 02:54:59 +01:00
|
|
|
return true;
|
2015-11-09 01:49:45 +01:00
|
|
|
}
|
2015-12-19 01:10:05 +01:00
|
|
|
|
2016-04-02 03:46:12 +02:00
|
|
|
public static function getUserBy($attr, $value, $inactive=false) {
|
|
|
|
$user = User::where($attr, $value);
|
|
|
|
|
|
|
|
if (!$inactive) {
|
|
|
|
// if user must be active
|
|
|
|
$user = $user
|
|
|
|
->where('active', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getUserById($user_id, $inactive=false) {
|
|
|
|
return self::getUserBy('id', $user_id, $inactive);
|
2015-12-19 01:10:05 +01:00
|
|
|
}
|
|
|
|
|
2016-03-23 02:54:59 +01:00
|
|
|
public static function getUserByUsername($username, $inactive=false) {
|
2016-04-02 03:46:12 +02:00
|
|
|
return self::getUserBy('username', $username, $inactive);
|
2016-03-23 02:54:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getUserByEmail($email, $inactive=false) {
|
2016-04-02 03:46:12 +02:00
|
|
|
return self::getUserBy('email', $email, $inactive);
|
2015-12-28 22:33:17 +01:00
|
|
|
}
|
2015-11-07 03:44:50 +01:00
|
|
|
}
|