Skip to content

A free open source IT asset/license management system

Better handle permissions with bad data

snipe6c6b37000ad4
diff --git a/app/Models/Group.php b/app/Models/Group.php
index 253d47fbb9..d4953291d2 100755
--- a/app/Models/Group.php
+++ b/app/Models/Group.php
@@ -76,7 +76,7 @@ class Group extends SnipeModel
*
* @author A. Gianotto <[email protected]>
* @since [v1.0]
- * @return array
+ * @return array | \stdClass
*/
public function decodePermissions()
{
@@ -84,20 +84,24 @@ class Group extends SnipeModel
if (is_array($this->permissions)) {
$this->permissions = json_encode($this->permissions);
}
- $permissions = json_decode($this->permissions ?? '{}', JSON_OBJECT_AS_ARRAY);
- // If there are no permissions, return an empty array
- if (!$permissions) {
- return [];
- }
+ $permissions = json_decode($this->permissions ?? '{}', JSON_OBJECT_AS_ARRAY);
// Otherwise, loop through the permissions and cast the values as integers
- foreach ($permissions as $permission => $value) {
- $permissions[$permission] = (int) $value;
+ if ((is_array($permissions)) && ($permissions)) {
+ foreach ($permissions as $permission => $value) {
+
+ if (!is_integer($permission)) {
+ $permissions[$permission] = (int) $value;
+ } else {
+ \Log::info('Weird data here - skipping it');
+ unset($permissions[$permission]);
+ }
+ }
+ return $permissions ?: new \stdClass;
}
+ return new \stdClass;
-
- return $permissions;
}
/**
diff --git a/app/Models/User.php b/app/Models/User.php
index 7d903aa137..b10a948849 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -746,24 +746,36 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
}
-
+ /**
+ * Decode JSON permissions into array
+ *
+ * @author A. Gianotto <[email protected]>
+ * @since [v1.0]
+ * @return array | \stdClass
+ */
public function decodePermissions()
{
// Set default to empty JSON if the value is null
- $permissions = json_decode($this->permissions ?? '{}', JSON_OBJECT_AS_ARRAY);
-
- // If there are no permissions, return an empty array
- if (!$permissions) {
- return [];
+ if (is_array($this->permissions)) {
+ $this->permissions = json_encode($this->permissions);
}
+ $permissions = json_decode($this->permissions ?? '{}', JSON_OBJECT_AS_ARRAY);
+
// Otherwise, loop through the permissions and cast the values as integers
- foreach ($permissions as $permission => $value) {
- $permissions[$permission] = (int) $value;
+ if ((is_array($permissions)) && ($permissions)) {
+ foreach ($permissions as $permission => $value) {
+
+ if (!is_integer($permission)) {
+ $permissions[$permission] = (int) $value;
+ } else {
+ \Log::info('Weird data here - skipping it');
+ unset($permissions[$permission]);
+ }
+ }
+ return $permissions ?: new \stdClass;
}
-
-
- return $permissions;
+ return new \stdClass;
}
/**