add services + more structural additions
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Da\User\Helper;
|
||||
|
||||
use Da\User\Module;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
@ -22,12 +23,24 @@ class AuthHelper
|
||||
*/
|
||||
public function hasRole($userId, $role)
|
||||
{
|
||||
if (Yii::$app->authManager) {
|
||||
$roles = array_keys(Yii::$app->authManager->getRolesByUser($userId));
|
||||
if (Yii::$app->getAuthManager()) {
|
||||
$roles = array_keys(Yii::$app->getAuthManager()->getRolesByUser($userId));
|
||||
|
||||
return in_array($role, $roles, true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isAdmin($username)
|
||||
{
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('user');
|
||||
$hasAdministratorPermissionName = Yii::$app->getAuthManager() && $module->administratorPermissionName
|
||||
? Yii::$app->getUser()->can($module->administratorPermissionName)
|
||||
: false;
|
||||
|
||||
return $hasAdministratorPermissionName || in_array($username, $module->administrators);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
60
lib/User/Helper/SecurityHelper.php
Normal file
60
lib/User/Helper/SecurityHelper.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Helper;
|
||||
|
||||
use yii\base\Security;
|
||||
|
||||
class SecurityHelper
|
||||
{
|
||||
/**
|
||||
* @var Security
|
||||
*/
|
||||
protected $security;
|
||||
|
||||
public function __construct(Security $security)
|
||||
{
|
||||
$this->security = $security;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a secure hash from a password and a random salt.
|
||||
*
|
||||
* @param string $password
|
||||
* @param null|int $cost
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generatePasswordHash($password, $cost = null)
|
||||
{
|
||||
return $this->security->generatePasswordHash($password, $cost);
|
||||
}
|
||||
|
||||
public function validatePassword($password, $hash)
|
||||
{
|
||||
return $this->security->validatePassword($password, $hash);
|
||||
}
|
||||
|
||||
public function generatePassword($length)
|
||||
{
|
||||
$sets = [
|
||||
'abcdefghjkmnpqrstuvwxyz',
|
||||
'ABCDEFGHJKMNPQRSTUVWXYZ',
|
||||
'23456789',
|
||||
];
|
||||
$all = '';
|
||||
$password = '';
|
||||
foreach ($sets as $set) {
|
||||
$password .= $set[array_rand(str_split($set))];
|
||||
$all .= $set;
|
||||
}
|
||||
|
||||
$all = str_split($all);
|
||||
for ($i = 0; $i < $length - count($sets); $i++) {
|
||||
$password .= $all[array_rand($all)];
|
||||
}
|
||||
|
||||
$password = str_shuffle($password);
|
||||
|
||||
return $password;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user