Add new class methods and use them

This commit is contained in:
FusionPBX
2023-09-11 01:45:19 -06:00
committed by GitHub
parent 6411f50750
commit 2c5724c6ed
8 changed files with 238 additions and 184 deletions
+66 -1
View File
@@ -25,7 +25,6 @@
sreis
*/
/**
* domains class
*
@@ -803,6 +802,72 @@ if (!class_exists('domains')) {
}
} //end settings method
/**
* get a domain list
*/
public function all() {
//get the domains from the database
$database = new database;
if ($database->table_exists('v_domains')) {
$sql = "select * from v_domains order by domain_name asc;";
$database = new database;
$result = $database->select($sql, null, 'all');
foreach($result as $row) {
$domain_names[] = $row['domain_name'];
}
unset($prep_statement);
}
//build the domains array in the correct order
if (is_array($domain_names)) {
foreach ($domain_names as $dn) {
foreach ($result as $row) {
if ($row['domain_name'] == $dn) {
$domains[] = $row;
}
}
}
unset($result);
}
//return the domains array
return $domains;
}
/**
* get a domain list
*/
public function session() {
//get the list of domains
$domains = $this->all();
//get the domain
$domain_array = explode(":", $_SERVER["HTTP_HOST"] ?? '');
//set domain_name and domain_uuid and update domains array with domain_uuid as the key
if (!empty($domains) && is_array($domains)) {
foreach($domains as $row) {
if (!isset($_SESSION['username'])) {
if (!empty($domains) && count($domains) == 1) {
$domain_uuid = $row["domain_uuid"];
$domain_name = $row['domain_name'];
$_SESSION["domain_uuid"] = $row["domain_uuid"];
$_SESSION["domain_name"] = $row['domain_name'];
}
else {
if ($row['domain_name'] == $domain_array[0] || $row['domain_name'] == 'www.'.$domain_array[0]) {
$_SESSION["domain_uuid"] = $row["domain_uuid"];
$_SESSION["domain_name"] = $row["domain_name"];
}
}
}
$_SESSION['domains'][$row['domain_uuid']] = $row;
}
unset($domains, $prep_statement);
}
}
}
}
+50
View File
@@ -470,6 +470,56 @@ if (!class_exists('groups')) {
}
}
/**
* get the groups assigned to the user
*/
public function assigned($domain_uuid, $user_uuid) {
$sql = "select ";
$sql .= "u.user_group_uuid, ";
$sql .= "u.domain_uuid, ";
$sql .= "u.user_uuid, ";
$sql .= "u.group_uuid, ";
$sql .= "g.group_name, ";
$sql .= "g.group_level ";
$sql .= "from ";
$sql .= "v_user_groups as u, ";
$sql .= "v_groups as g ";
$sql .= "where u.domain_uuid = :domain_uuid ";
$sql .= "and u.user_uuid = :user_uuid ";
$sql .= "and u.group_uuid = g.group_uuid ";
$parameters['domain_uuid'] = $domain_uuid;
$parameters['user_uuid'] = $user_uuid;
$database = new database;
$groups = $database->select($sql, $parameters, 'all');
unset($sql, $parameters);
if (!empty($groups)) {
return $groups;
}
else {
return false;
}
}
/**
* add the assigned groups the session array
*/
public function session($domain_uuid, $user_uuid) {
//get the groups
$groups = $this->assigned($domain_uuid, $user_uuid);
//set the groups in the session
$_SESSION["groups"] = $groups;
$_SESSION["user"]["groups"] = $groups;
//get the users group level
$_SESSION["user"]["group_level"] = 0;
foreach ($_SESSION['user']['groups'] as $row) {
if ($_SESSION["user"]["group_level"] < $row['group_level']) {
$_SESSION["user"]["group_level"] = $row['group_level'];
}
}
}
}
}
+80 -8
View File
@@ -18,7 +18,6 @@
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Copyright (C) 2016 All Rights Reserved.
*/
/**
@@ -65,16 +64,89 @@ if (!class_exists('permissions')) {
* Check to see if the permission exists
* @var string $permission
*/
function exists($permission) {
//set default false
$result = false;
//search for the permission
if (!empty($_SESSION["permissions"]) && is_array($_SESSION["permissions"]) && isset($_SESSION["permissions"][$permission])) {
$result = true;
public function exists($permission) {
//define permissions global variable
global $permissions;
if (empty($permissions) && empty($_SESSION["permissions"])) {
//define additional global variables
global $groups, $domain_uuid, $user_uuid;
//get the groups assigned to the user
if (empty($groups)) {
$group = new groups;
$groups = $group->assigned($domain_uuid, $user_uuid);
}
//get the permissions assigned to the user through the assigned groups
$permission = new permissions;
$permissions = $permission->assigned($domain_uuid, $groups);
}
if (!empty($_SESSION["permissions"])) {
$permissions = $_SESSION["permissions"];
}
//set default to false
$result = false;
//search for the permission
if (!empty($permissions) && !empty($permission)) {
foreach($permissions as $field) {
if ($permission == $field) {
$result = true;
}
}
}
//return the result
return $result;
return $result;
}
/**
* get the assigned permissions
* @var array $groups
*/
public function assigned($domain_uuid, $groups) {
//groups not provided return false
if (empty($groups)) {
return false;
}
//get the permissions assigned to the user through the assigned groups
$x = 0;
$sql = "select distinct(permission_name) from v_group_permissions ";
$sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
foreach ($groups as $field) {
if (!empty($field['group_name'])) {
$sql_where_or[] = "group_name = :group_name_".$x;
$parameters['group_name_'.$x] = $field['group_name'];
$x++;
}
}
if (!empty($sql_where_or)) {
$sql .= "and (".implode(' or ', $sql_where_or).") ";
}
$sql .= "and permission_assigned = 'true' ";
$parameters['domain_uuid'] = $domain_uuid;
$database = new database;
$permissions = $database->select($sql, $parameters, 'all');
unset($sql, $parameters, $result);
return $permissions;
}
/**
* save the assigned permissions to a session
*/
public function session($domain_uuid, $groups) {
$permissions = $this->assigned($domain_uuid, $groups);
if (!empty($permissions)) {
foreach ($permissions as $row) {
$_SESSION['permissions'][$row["permission_name"]] = true;
$_SESSION["user"]["permissions"][$row["permission_name"]] = true;
}
}
}
}
}