2011-06-02 00:48:29 +02:00
|
|
|
<?php
|
|
|
|
|
2018-10-11 01:26:34 +02:00
|
|
|
use Carbon\Carbon;
|
2017-01-21 13:58:53 +01:00
|
|
|
use Engelsystem\Database\DB;
|
2018-10-11 01:26:34 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2017-01-21 13:58:53 +01:00
|
|
|
|
2016-09-29 11:28:42 +02:00
|
|
|
/**
|
|
|
|
* Testet ob ein User eingeloggt ist und lädt die entsprechenden Privilegien
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function load_auth()
|
|
|
|
{
|
2018-10-11 01:26:34 +02:00
|
|
|
global $privileges;
|
2017-08-30 19:57:01 +02:00
|
|
|
$session = session();
|
|
|
|
|
|
|
|
if ($session->has('uid')) {
|
2018-10-11 01:26:34 +02:00
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
if ($user) {
|
|
|
|
$user->last_login_at = new Carbon();
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$privileges = privileges_for_user($user->id);
|
2017-01-02 03:57:23 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-08-30 19:57:01 +02:00
|
|
|
|
|
|
|
$session->remove('uid');
|
2016-09-29 11:28:42 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
// guest privileges
|
2017-08-29 22:22:53 +02:00
|
|
|
$privileges = privileges_for_group(-10);
|
2011-06-02 00:48:29 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 11:28:42 +02:00
|
|
|
/**
|
|
|
|
* generate a salt (random string) of arbitrary length suitable for the use with crypt()
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param int $length
|
|
|
|
* @return string
|
2016-09-29 11:28:42 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function generate_salt($length = 16)
|
|
|
|
{
|
2017-01-03 14:12:17 +01:00
|
|
|
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
|
|
$salt = '';
|
2017-01-02 15:43:36 +01:00
|
|
|
for ($i = 0; $i < $length; $i++) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$salt .= $alphabet[rand(0, strlen($alphabet) - 1)];
|
|
|
|
}
|
|
|
|
return $salt;
|
2012-12-12 02:31:54 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 11:28:42 +02:00
|
|
|
/**
|
|
|
|
* set the password of a user
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param int $uid
|
|
|
|
* @param string $password
|
2016-09-29 11:28:42 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function set_password($uid, $password)
|
|
|
|
{
|
2018-10-11 01:26:34 +02:00
|
|
|
$user = User::find($uid);
|
|
|
|
$user->password = crypt($password, config('crypt_alg') . '$' . generate_salt(16) . '$');
|
|
|
|
$user->save();
|
2012-12-12 02:31:54 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 11:28:42 +02:00
|
|
|
/**
|
|
|
|
* verify a password given a precomputed salt.
|
|
|
|
* if $uid is given and $salt is an old-style salt (plain md5), we convert it automatically
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @param string $salt
|
|
|
|
* @param int $uid
|
|
|
|
* @return bool
|
2016-09-29 11:28:42 +02:00
|
|
|
*/
|
2017-01-03 03:22:48 +01:00
|
|
|
function verify_password($password, $salt, $uid = null)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2017-01-21 23:07:20 +01:00
|
|
|
$crypt_alg = config('crypt_alg');
|
2017-01-02 03:57:23 +01:00
|
|
|
$correct = false;
|
2017-12-25 23:12:52 +01:00
|
|
|
if (substr($salt, 0, 1) == '$') {
|
|
|
|
// new-style crypt()
|
2017-01-02 15:43:36 +01:00
|
|
|
$correct = crypt($password, $salt) == $salt;
|
2017-12-25 23:12:52 +01:00
|
|
|
} elseif (substr($salt, 0, 7) == '{crypt}') {
|
|
|
|
// old-style crypt() with DES and static salt - not used anymore
|
2017-01-02 15:43:36 +01:00
|
|
|
$correct = crypt($password, '77') == $salt;
|
2017-12-25 23:12:52 +01:00
|
|
|
} elseif (strlen($salt) == 32) {
|
|
|
|
// old-style md5 without salt - not used anymore
|
2017-01-02 15:43:36 +01:00
|
|
|
$correct = md5($password) == $salt;
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-01 18:49:43 +01:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
if ($correct && substr($salt, 0, strlen($crypt_alg)) != $crypt_alg && intval($uid)) {
|
2017-01-02 03:57:23 +01:00
|
|
|
// this password is stored in another format than we want it to be.
|
2017-01-02 15:43:36 +01:00
|
|
|
// let's update it!
|
2017-12-25 23:12:52 +01:00
|
|
|
// we duplicate the query from the above set_password() function to have the extra safety of checking
|
|
|
|
// the old hash
|
2018-10-11 01:26:34 +02:00
|
|
|
$user = User::find($uid);
|
|
|
|
if ($user->password == $salt) {
|
|
|
|
$user->password = crypt($password, $crypt_alg . '$' . generate_salt() . '$');
|
|
|
|
$user->save();
|
|
|
|
}
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return $correct;
|
2011-06-02 00:48:29 +02:00
|
|
|
}
|
2011-06-03 15:30:17 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param int $user_id
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function privileges_for_user($user_id)
|
|
|
|
{
|
|
|
|
$privileges = [];
|
2017-01-21 13:58:53 +01:00
|
|
|
$user_privileges = DB::select('
|
2017-01-02 15:43:36 +01:00
|
|
|
SELECT `Privileges`.`name`
|
2018-10-11 01:26:34 +02:00
|
|
|
FROM `users`
|
|
|
|
JOIN `UserGroups` ON (`users`.`id` = `UserGroups`.`uid`)
|
2017-01-02 15:43:36 +01:00
|
|
|
JOIN `GroupPrivileges` ON (`UserGroups`.`group_id` = `GroupPrivileges`.`group_id`)
|
|
|
|
JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`)
|
2018-10-11 01:26:34 +02:00
|
|
|
WHERE `users`.`id`=?
|
2017-01-21 13:58:53 +01:00
|
|
|
', [$user_id]);
|
|
|
|
foreach ($user_privileges as $user_privilege) {
|
|
|
|
$privileges[] = $user_privilege['name'];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return $privileges;
|
2011-06-03 15:30:17 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param int $group_id
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function privileges_for_group($group_id)
|
|
|
|
{
|
|
|
|
$privileges = [];
|
2017-01-21 13:58:53 +01:00
|
|
|
$groups_privileges = DB::select('
|
|
|
|
SELECT `name`
|
2017-01-02 15:43:36 +01:00
|
|
|
FROM `GroupPrivileges`
|
|
|
|
JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`)
|
2017-01-21 13:58:53 +01:00
|
|
|
WHERE `group_id`=?
|
|
|
|
', [$group_id]);
|
|
|
|
foreach ($groups_privileges as $guest_privilege) {
|
|
|
|
$privileges[] = $guest_privilege['name'];
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return $privileges;
|
2011-06-03 15:30:17 +02:00
|
|
|
}
|