update folder location
This commit is contained in:
112
src/User/Helper/AuthHelper.php
Normal file
112
src/User/Helper/AuthHelper.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Helper;
|
||||
|
||||
use Da\User\Model\AbstractAuthItem;
|
||||
use Da\User\Module;
|
||||
use Da\User\Traits\AuthManagerAwareTrait;
|
||||
use Yii;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\rbac\Permission;
|
||||
use yii\rbac\Role;
|
||||
use yii\rbac\Rule;
|
||||
|
||||
class AuthHelper
|
||||
{
|
||||
use AuthManagerAwareTrait;
|
||||
|
||||
/**
|
||||
* Checks whether a user has certain role.
|
||||
*
|
||||
* @param $userId
|
||||
* @param $role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRole($userId, $role)
|
||||
{
|
||||
if ($this->getAuthManager()) {
|
||||
$roles = array_keys($this->getAuthManager()->getRolesByUser($userId));
|
||||
|
||||
return in_array($role, $roles, true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $username
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAdmin($username)
|
||||
{
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('user');
|
||||
$hasAdministratorPermissionName = $this->getAuthManager() && $module->administratorPermissionName
|
||||
? Yii::$app->getUser()->can($module->administratorPermissionName)
|
||||
: false;
|
||||
|
||||
return $hasAdministratorPermissionName || in_array($username, $module->administrators);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return null|\yii\rbac\Item|Permission
|
||||
*/
|
||||
public function getPermission($name)
|
||||
{
|
||||
return $this->getAuthManager()->getPermission($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return null|\yii\rbac\Item|Role
|
||||
*/
|
||||
public function getRole($name)
|
||||
{
|
||||
return $this->getAuthManager()->getRole($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a role, permission or rule from the RBAC system.
|
||||
*
|
||||
* @param Role|Permission|Rule $object
|
||||
*
|
||||
* @return bool whether the role, permission or rule is successfully removed
|
||||
*/
|
||||
public function remove($object)
|
||||
{
|
||||
return $this->getAuthManager()->remove($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractAuthItem $model
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUnassignedItems(AbstractAuthItem $model)
|
||||
{
|
||||
$excludeItems = $model->item !== null ? [$model->item->name] : [];
|
||||
$items = $this->getAuthManager()->getItems($model->getType(), $excludeItems);
|
||||
|
||||
return ArrayHelper::map(
|
||||
$items,
|
||||
'name',
|
||||
function ($item) {
|
||||
return empty($item->description) ? $item->name : "{$item->name} ({$item->description})";
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
59
src/User/Helper/ClassMapHelper.php
Normal file
59
src/User/Helper/ClassMapHelper.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Helper;
|
||||
|
||||
/**
|
||||
* ModelMapHelper.php.
|
||||
*
|
||||
* Date: 3/12/16
|
||||
* Time: 18:10
|
||||
*
|
||||
* @author Antonio Ramirez <hola@2amigos.us>
|
||||
*/
|
||||
class ClassMapHelper
|
||||
{
|
||||
protected $map = [];
|
||||
|
||||
/**
|
||||
* ModelClassMapHelper constructor.
|
||||
*
|
||||
* @param array $map
|
||||
*/
|
||||
public function __construct($map = [])
|
||||
{
|
||||
$this->map = $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $class
|
||||
*/
|
||||
public function set($key, $class)
|
||||
{
|
||||
$this->map[$key] = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
if (array_key_exists($key, $this->map)) {
|
||||
return $this->map[$key];
|
||||
}
|
||||
throw new \Exception('Unknown model map key: '.$key);
|
||||
}
|
||||
}
|
||||
25
src/User/Helper/GravatarHelper.php
Normal file
25
src/User/Helper/GravatarHelper.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Helper;
|
||||
|
||||
class GravatarHelper
|
||||
{
|
||||
public function buildId($email)
|
||||
{
|
||||
return md5(strtolower(trim($email)));
|
||||
}
|
||||
|
||||
public function getUrl($id, $size = 200)
|
||||
{
|
||||
return '//gravatar.com/avatar/'.$id.'?s='.$size;
|
||||
}
|
||||
}
|
||||
74
src/User/Helper/SecurityHelper.php
Normal file
74
src/User/Helper/SecurityHelper.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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 generateRandomString($length = 32)
|
||||
{
|
||||
return $this->security->generateRandomString($length);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
44
src/User/Helper/TimezoneHelper.php
Normal file
44
src/User/Helper/TimezoneHelper.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Helper;
|
||||
|
||||
use DateTimeZone;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use DateTime;
|
||||
|
||||
class TimezoneHelper
|
||||
{
|
||||
/**
|
||||
* Get all of the time zones with the offsets sorted by their offset.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAll()
|
||||
{
|
||||
$timeZones = [];
|
||||
$timeZoneIdentifiers = DateTimeZone::listIdentifiers();
|
||||
|
||||
foreach ($timeZoneIdentifiers as $timeZone) {
|
||||
$date = new DateTime('now', new DateTimeZone($timeZone));
|
||||
$offset = $date->getOffset() / 60 / 60;
|
||||
$timeZones[] = [
|
||||
'timezone' => $timeZone,
|
||||
'name' => "{$timeZone} (UTC ".($offset > 0 ? '+' : '')."{$offset})",
|
||||
'offset' => $offset,
|
||||
];
|
||||
}
|
||||
|
||||
ArrayHelper::multisort($timeZones, 'offset', SORT_DESC, SORT_NUMERIC);
|
||||
|
||||
return $timeZones;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user