Skip to content

Commit

Permalink
1.0.4
Browse files Browse the repository at this point in the history
Hotfixing permission helper issue
Adding a crypto helper.
Improving permissions helper.
gustavs-gutmanis committed Feb 25, 2018
1 parent bd866ab commit 9313bfe
Showing 3 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "solspace/craft3-commons",
"description": "Common object library for Solspace projects",
"version": "1.0.3",
"version": "1.0.4",
"type": "library",
"license": "MIT",
"minimum-stability": "dev",
66 changes: 66 additions & 0 deletions src/Helpers/CryptoHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Created by PhpStorm.
* User: gustavs
* Date: 18.14.2
* Time: 13:37
*/

namespace Solspace\Commons\Helpers;

class CryptoHelper
{
/**
* Generate a unique token
*
* @param int $length
*
* @return string
*/
public static function getUniqueToken(int $length = 40): string
{
$token = '';
$codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz';
$codeAlphabet .= '0123456789';
$max = strlen($codeAlphabet); // edited

for ($i = 0; $i < $length; $i++) {
$token .= $codeAlphabet[self::getSecureRandomInt(0, $max - 1)];
}

return $token;
}

/**
* Generate a secure random int
*
* @param int $min
* @param int $max
*
* @return int
*/
public static function getSecureRandomInt(int $min, int $max): int
{
if (function_exists('random_int')) {
return random_int($min, $max);
}

$range = $max - $min;

if ($range < 1) {
return $min; // not so random...
}

$log = ceil(log($range, 2));
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd > $range);

return $min + $rnd;
}
}
22 changes: 21 additions & 1 deletion src/Helpers/PermissionHelper.php
Original file line number Diff line number Diff line change
@@ -16,11 +16,19 @@ class PermissionHelper
*/
public static function checkPermission(string $permissionName, bool $checkForNested = false): bool
{
if (self::isAdmin()) {
return true;
}

$user = \Craft::$app->getUser();
$permissionName = strtolower($permissionName);

if (self::permissionsEnabled()) {
if ($checkForNested) {
if (!$user->getId()) {
return false;
}

$permissionList = \Craft::$app->userPermissions->getPermissionsByUserId($user->getId());
foreach ($permissionList as $permission) {
if (strpos($permission, $permissionName) === 0) {
@@ -32,7 +40,7 @@ public static function checkPermission(string $permissionName, bool $checkForNes
return $user->checkPermission($permissionName);
}

return self::isAdmin();
return false;
}

/**
@@ -64,14 +72,26 @@ public static function requirePermission(string $permissionName)
*/
public static function getNestedPermissionIds(string $permissionName)
{
if (self::isAdmin()) {
return true;
}

$user = \Craft::$app->getUser();
$permissionName = strtolower($permissionName);
$idList = [];

if (self::permissionsEnabled()) {
if (!$user->getId()) {
return [];
}

$permissionList = \Craft::$app->userPermissions->getPermissionsByUserId($user->getId());
foreach ($permissionList as $permission) {
if (strpos($permission, $permissionName) === 0) {
if (strpos($permission, ':') === false) {
continue;
}

list($name, $id) = explode(':', $permission);

$idList[] = $id;

0 comments on commit 9313bfe

Please sign in to comment.