2015-11-07 03:44:50 +01:00
|
|
|
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\User;
|
|
|
|
use Hash;
|
|
|
|
|
|
|
|
class UserHelper {
|
|
|
|
public static function userExists($username) {
|
|
|
|
$user = User::where('active', 1)
|
|
|
|
->where('username', $username)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
return ($user ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function emailExists($email) {
|
|
|
|
$user = User::where('active', 1)
|
|
|
|
->where('email', $email)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
return ($user ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateUsername($username) {
|
|
|
|
return ctype_alnum($username);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateEmail($email) {
|
|
|
|
// TODO validate email here
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
public static function createNewRecoveryKey($username) {
|
|
|
|
$recovery_key = CryptoHelper::generateRandomHex(50);
|
|
|
|
|
|
|
|
$user = User::where('active', 1)
|
|
|
|
->where('username', $username)
|
|
|
|
->first();
|
2015-12-19 01:10:05 +01:00
|
|
|
|
2015-11-09 01:49:45 +01:00
|
|
|
if ($user == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->recovery_key = $recovery_key;
|
|
|
|
$user->save();
|
|
|
|
}
|
2015-12-19 01:10:05 +01:00
|
|
|
|
|
|
|
public static function getUserById($user_id) {
|
|
|
|
$user = User::where('id', $user_id)
|
|
|
|
->where('active', 1)
|
|
|
|
->first();
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2015-11-07 03:44:50 +01:00
|
|
|
}
|