1
0
mirror of https://github.com/cydrobolt/polr.git synced 2024-11-09 19:52:28 +01:00
polr/app/Helpers/UserHelper.php

110 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Helpers;
2016-03-23 02:54:59 +01:00
use App\Models\User;
2016-03-23 02:54:59 +01:00
use App\Helpers\CryptoHelper;
use Hash;
class UserHelper {
public static $USER_ROLES = [
'admin' => 'admin',
'default' => '',
];
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);
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);
return ($user ? true : false);
}
public static function validateUsername($username) {
return ctype_alnum($username);
}
public static function userIsAdmin($username) {
return (self::getUserByUsername($username)->role == self::$USER_ROLES['admin']);
}
public static function checkCredentials($username, $password) {
$user = User::where('active', 1)
->where('username', $username)
->first();
if ($user == null) {
return false;
}
$correct_password = Hash::check($password, $user->password);
if (!$correct_password) {
return false;
}
else {
return ['username' => $username, 'role' => $user->role];
}
}
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);
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
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
}
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);
}
2016-03-23 02:54:59 +01:00
public static function getUserByUsername($username, $inactive=false) {
return self::getUserBy('username', $username, $inactive);
2016-03-23 02:54:59 +01:00
}
public static function getUserByEmail($email, $inactive=false) {
return self::getUserBy('email', $email, $inactive);
2015-12-28 22:33:17 +01:00
}
}