Page MenuHomeDevCentral

D3857.diff
No OneTemporary

D3857.diff

diff --git a/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php b/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php
--- a/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php
+++ b/workspaces/src/Engines/Auth/Actions/GivePermissionUserAction.php
@@ -57,7 +57,10 @@
* Executes the user action
*/
public function run () {
- $id = Resources::resolveID($this->resourceType, $this->resourceIdentifier);
+ $id = $this->context->resources->resolveID(
+ $this->resourceType,
+ $this->resourceIdentifier,
+ );
if ($id->isNone()) {
throw new Exception("Can't resolve resource "
diff --git a/workspaces/src/Engines/Auth/AuthenticationMethod.php b/workspaces/src/Engines/Auth/AuthenticationMethod.php
--- a/workspaces/src/Engines/Auth/AuthenticationMethod.php
+++ b/workspaces/src/Engines/Auth/AuthenticationMethod.php
@@ -20,7 +20,7 @@
use Waystone\Workspaces\Engines\Auth\Actions\AddToGroupUserAction;
use Waystone\Workspaces\Engines\Auth\Actions\GivePermissionUserAction;
use Waystone\Workspaces\Engines\Framework\Context;
-use Waystone\Workspaces\Engines\Serialization\ArrayDeserializable;
+use Waystone\Workspaces\Engines\Serialization\ArrayDeserializableWithContext;
use Keruald\OmniTools\DataTypes\Option\None;
use Keruald\OmniTools\DataTypes\Option\Option;
@@ -38,7 +38,7 @@
*
* This class has to be extended to implement custom authentication methods.
*/
-abstract class AuthenticationMethod implements ArrayDeserializable {
+abstract class AuthenticationMethod implements ArrayDeserializableWithContext {
/**
* @var User The local user matching the authentication
@@ -107,6 +107,7 @@
*/
protected function runCreateUserActions () {
foreach ($this->createUserActions as $action) {
+ $action->context = $this->context;
$action->targetUser = $this->localUser;
$action->run();
}
@@ -118,18 +119,21 @@
* @return Option<User> the user if a user has been found; otherwise, false.
*/
private function findUser () : Option {
+ $users = $this->context->userRepository;
+
if ($this->remoteUserId != '') {
- $user = User::getUserFromRemoteIdentity(
- $this->id, $this->remoteUserId,
- );
+ $user = $users->getUserFromRemoteIdentity(
+ $this->id, $this->remoteUserId,
+ );
- if ($user !== null) {
- return new Some($user);
+ if ($user->isSome()) {
+ return $user;
}
}
if ($this->email != '') {
- $user = User::get_user_from_email($this->email);
+ $user = $users->getUserFromEmail($this->email);
+
if ($user->isSome()) {
return $user;
}
@@ -198,7 +202,7 @@
throw new Exception("Can't create user: the canCreateUser property is set at false.");
}
- $user = User::create();
+ $user = User::create($this->context->db);
$user->name = $this->name;
$user->email = $this->email;
$user->save_to_database();
@@ -240,13 +244,16 @@
* Typically used to deserialize a configuration.
*
* @param array $data The associative array to deserialize
+ * @param mixed $context The application context
*
* @return AuthenticationMethod The deserialized instance
* @throws InvalidArgumentException|Exception
*/
- public static function loadFromArray (array $data) : self {
+ public static function loadFromArray (array $data, mixed $context) : self {
$instance = new static;
+ $instance->context = $context;
+
if (!array_key_exists("id", $data)) {
throw new InvalidArgumentException("Authentication method id is required.");
}
diff --git a/workspaces/src/Engines/Auth/Methods/AzharProvider.php b/workspaces/src/Engines/Auth/Methods/AzharProvider.php
--- a/workspaces/src/Engines/Auth/Methods/AzharProvider.php
+++ b/workspaces/src/Engines/Auth/Methods/AzharProvider.php
@@ -210,11 +210,12 @@
* Typically used to deserialize a configuration.
*
* @param array $data The associative array to deserialize
+ * @param mixed $context The application context
*
* @return AzharProvider The deserialized instance
*/
- public static function loadFromArray (array $data) : self {
- $instance = parent::loadFromArray($data);
+ public static function loadFromArray (array $data, mixed $context) : self {
+ $instance = parent::loadFromArray($data, $context);
$instance->url = $data["url"];
$instance->secretKey = $data["secretKey"];
diff --git a/workspaces/src/Engines/Auth/UserAction.php b/workspaces/src/Engines/Auth/UserAction.php
--- a/workspaces/src/Engines/Auth/UserAction.php
+++ b/workspaces/src/Engines/Auth/UserAction.php
@@ -18,6 +18,8 @@
namespace Waystone\Workspaces\Engines\Auth;
+use Waystone\Workspaces\Engines\Framework\Context;
+
use User;
/**
@@ -30,6 +32,8 @@
*/
public $targetUser;
+ public ?Context $context = null;
+
/**
* Initializes a new instance of an UserAction object
*
diff --git a/workspaces/src/Engines/Framework/Application.php b/workspaces/src/Engines/Framework/Application.php
--- a/workspaces/src/Engines/Framework/Application.php
+++ b/workspaces/src/Engines/Framework/Application.php
@@ -4,6 +4,7 @@
use Keruald\Database\Database;
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
+use Waystone\Workspaces\Engines\Users\UserRepository;
class Application {
@@ -17,7 +18,14 @@
$context->config = $config;
$context->db = Database::load($config["sql"]);
- $context->session = Session::load($context->db);
+ $context->userRepository = new UserRepository($context->db);
+ $context->resources = new Resources(
+ $context->userRepository,
+ );
+ $context->session = Session::load(
+ $context->db,
+ $context->userRepository,
+ );
$context->url = get_current_url_fragments();
$context->initializeTemplateEngine($context->config['Theme']);
diff --git a/workspaces/src/Engines/Framework/Context.php b/workspaces/src/Engines/Framework/Context.php
--- a/workspaces/src/Engines/Framework/Context.php
+++ b/workspaces/src/Engines/Framework/Context.php
@@ -20,6 +20,7 @@
use Keruald\Database\DatabaseEngine;
use Smarty\Smarty;
use User;
+use Waystone\Workspaces\Engines\Users\UserRepository;
use Waystone\Workspaces\Engines\Workspaces\WorkSpace;
/**
@@ -44,6 +45,13 @@
*/
public array $config;
+ /**
+ * @var UserRepository the users already loaded from database
+ */
+ public UserRepository $userRepository;
+
+ public Resources $resources;
+
/**
* @var User the user currently logged in
*/
diff --git a/workspaces/src/Engines/Framework/Repository.php b/workspaces/src/Engines/Framework/Repository.php
new file mode 100644
--- /dev/null
+++ b/workspaces/src/Engines/Framework/Repository.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Waystone\Workspaces\Engines\Framework;
+
+use Keruald\Database\DatabaseEngine;
+use Keruald\OmniTools\Collections\HashMap;
+use Keruald\OmniTools\DataTypes\Option\Option;
+
+abstract class Repository {
+
+ ///
+ /// Properties
+ ///
+
+ protected DatabaseEngine $db;
+
+ /**
+ * @var HashMap A map of objects already loaded from the database
+ */
+ protected HashMap $table;
+
+ ///
+ /// Constructor
+ ///
+
+ public function __construct (DatabaseEngine $db) {
+ $this->db = $db;
+ $this->table = new HashMap();
+ }
+
+ ///
+ /// Table
+ ///
+
+ protected function lookupInTable (string $property, string $value) : Option {
+ return $this->table
+ ->filter(fn($item) => $item->$property == $value)
+ ->first();
+ }
+
+}
diff --git a/workspaces/src/Engines/Framework/Resources.php b/workspaces/src/Engines/Framework/Resources.php
--- a/workspaces/src/Engines/Framework/Resources.php
+++ b/workspaces/src/Engines/Framework/Resources.php
@@ -2,23 +2,28 @@
namespace Waystone\Workspaces\Engines\Framework;
+use Waystone\Workspaces\Engines\Users\UserRepository;
use Waystone\Workspaces\Engines\Workspaces\Workspace;
use Keruald\OmniTools\DataTypes\Option\None;
use Keruald\OmniTools\DataTypes\Option\Option;
use Keruald\OmniTools\DataTypes\Option\Some;
-use User;
use UserGroup;
use InvalidArgumentException;
class Resources {
+ public function __construct (
+ private UserRepository $users,
+ ) {
+ }
+
/**
* @return Option<int>
*/
- public static function resolveID (string $resource_type, string $identifier) : Option {
+ public function resolveID (string $resource_type, string $identifier) : Option {
//Trivial cases: already an ID, null or void ID
if (is_numeric($identifier)) {
return new Some((int)$identifier);
@@ -31,7 +36,7 @@
//Searches identifier
switch ($resource_type) {
case 'U':
- return User::resolveUserID($identifier);
+ return $this->users->resolveUserID($identifier);
case 'G':
$group = UserGroup::fromCode($identifier);
diff --git a/workspaces/src/Engines/Framework/Session.php b/workspaces/src/Engines/Framework/Session.php
--- a/workspaces/src/Engines/Framework/Session.php
+++ b/workspaces/src/Engines/Framework/Session.php
@@ -21,9 +21,12 @@
namespace Waystone\Workspaces\Engines\Framework;
+use Waystone\Workspaces\Engines\Errors\ErrorHandling;
+use Waystone\Workspaces\Engines\Users\UserRepository;
+
use Keruald\Database\DatabaseEngine;
+
use User;
-use Waystone\Workspaces\Engines\Errors\ErrorHandling;
/**
* Session class
@@ -42,6 +45,8 @@
public DatabaseEngine $db;
+ private UserRepository $users;
+
/*
* @var Session current session instance
*/
@@ -52,9 +57,12 @@
*
* @return Session current session instance
*/
- public static function load (DatabaseEngine $db) {
+ public static function load (
+ DatabaseEngine $db,
+ UserRepository $users,
+ ) {
if (!isset(self::$instance)) {
- self::$instance = new self($db);
+ self::$instance = new self($db, $users);
}
return self::$instance;
@@ -63,8 +71,12 @@
/**
* Initializes a new instance of Session object
*/
- private function __construct (DatabaseEngine $db) {
+ private function __construct (
+ DatabaseEngine $db,
+ UserRepository $users,
+ ) {
$this->db = $db;
+ $this->users = $users;
//Starts PHP session, and gets id
session_start();
@@ -249,7 +261,7 @@
//Gets user instance
require_once('includes/objects/user.php');
- $user = new User($row['user_id']);
+ $user = new User($row['user_id'], $db);
//Adds session property to this user instance
$user->session = $row;
@@ -311,4 +323,3 @@
$this->clean();
}
}
-
diff --git a/workspaces/src/Engines/Users/UserRepository.php b/workspaces/src/Engines/Users/UserRepository.php
new file mode 100644
--- /dev/null
+++ b/workspaces/src/Engines/Users/UserRepository.php
@@ -0,0 +1,134 @@
+<?php
+
+namespace Waystone\Workspaces\Engines\Users;
+
+use Waystone\Workspaces\Engines\Errors\ErrorHandling;
+use Waystone\Workspaces\Engines\Framework\Repository;
+
+use Keruald\Database\Exceptions\SqlException;
+use Keruald\OmniTools\DataTypes\Option\None;
+use Keruald\OmniTools\DataTypes\Option\Option;
+use Keruald\OmniTools\DataTypes\Option\Some;
+
+use User;
+
+class UserRepository extends Repository {
+
+ ///
+ /// Find user in database
+ ///
+
+ public function resolveUserID (string $expression) : Option {
+ return $this->getUserFromUsername($expression)
+ ->orElse(fn() => $this->getUserFromEmail($expression))
+ ->map(fn($user) => $user->id);
+ }
+
+ /**
+ * @return Option<User>
+ */
+ private function getByProperty (string $property, mixed $value) : Option {
+ $value = $this->db->escape($value);
+ $sql = "SELECT * FROM " . TABLE_USERS . " WHERE $property = '$value'";
+ if (!$result = $this->db->query($sql)) {
+ ErrorHandling::messageAndDie(SQL_ERROR, "Can't get user", '', __LINE__, __FILE__, $sql);
+ }
+
+ $row = $this->db->fetchRow($result);
+
+ if (!$row) {
+ return new None;
+ }
+
+ $user = new User(null, $this->db);
+ $user->load_from_row($row);
+ $this->table[$user->id] = $user;
+
+ return new Some($user);
+ }
+
+ /**
+ * Gets user from specified e-mail
+ *
+ * @return Option<User> the user matching the specified e-mail; None, if the mail were not found.
+ */
+ public function getUserFromEmail (string $mail) : Option {
+ return $this->lookupInTable("email", $mail)
+ ->orElse(fn () => $this->getByProperty("user_email", $mail));
+ }
+
+ /**
+ * @return Option<User>
+ */
+ public function getUserFromUsername (string $username) : Option {
+ return $this->lookupInTable("name", $username)
+ ->orElse(fn () => $this->getByProperty("username", $username));
+ }
+
+ /**
+ * Gets user from remote identity provider identifiant
+ *
+ * @param string $authType The authentication method type
+ * @param string $remoteUserId The remote user identifier
+ * @return Option<User> the user matching the specified identity provider and identifiant; None if no user were found.
+ */
+ public function getUserFromRemoteIdentity (string $authType, string $remoteUserId) : Option {
+ $authType = $this->db->escape($authType);
+ $remoteUserId = $this->db->escape($remoteUserId);
+ $sql = "SELECT user_id FROM " . TABLE_USERS_AUTH . " WHERE "
+ . "auth_type = '$authType' AND auth_identity = '$remoteUserId'";
+
+ try {
+ $result = $this->db->queryScalar($sql);
+ } catch (SqlException $ex) {
+ ErrorHandling::messageAndDie(SQL_ERROR, $ex->getMessage(), "Can't get user", __LINE__, __FILE__, $sql);
+ }
+
+ return Option::from($result)
+ ->map(fn($user_id) => $this->get($user_id));
+ }
+
+ ///
+ /// Registration facilities
+ ///
+
+ /**
+ * Checks if a username is still available
+ */
+ public function isAvailableUsername (string $login) : bool {
+ $login = $this->db->escape($login);
+
+ $sql = "SELECT COUNT(*) FROM " . TABLE_USERS
+ . " WHERE username = '$login'";
+
+ try {
+ $result = $this->db->queryScalar($sql);
+ } catch (SqlException $ex) {
+ ErrorHandling::messageAndDie(SQL_ERROR, "Can't check if the specified login is available", '', __LINE__, __FILE__, $sql);
+ }
+
+ return $result == 0;
+ }
+
+ ///
+ /// Load object
+ ///
+
+ /**
+ * Gets an instance of the class from the table or loads it from database.
+ *
+ * @param int $id the user ID
+ * @return User the user instance
+ */
+ public function get (int $id) : User {
+ if ($this->table->has($id)) {
+ return $this->table[$id];
+ }
+
+ $user = new User($id, $this->db);
+ $this->table[$id] = $user;
+
+ return $user;
+ }
+
+}
diff --git a/workspaces/src/Engines/Workspaces/Workspace.php b/workspaces/src/Engines/Workspaces/Workspace.php
--- a/workspaces/src/Engines/Workspaces/Workspace.php
+++ b/workspaces/src/Engines/Workspaces/Workspace.php
@@ -227,7 +227,7 @@
$cache = Cache::load();
if (!$workspaces = unserialize($cache->get("workspaces-$user_id"))) {
- $clause = User::get_permissions_clause_from_user_id($user_id);
+ $clause = User::get_permissions_clause_from_user_id($user_id, $db);
$sql = "SELECT DISTINCT w.*
FROM " . TABLE_PERMISSIONS . " p, " . TABLE_WORKSPACES . " w
WHERE p.target_resource_type = 'W' AND
diff --git a/workspaces/src/Engines/Workspaces/WorkspaceConfiguration.php b/workspaces/src/Engines/Workspaces/WorkspaceConfiguration.php
--- a/workspaces/src/Engines/Workspaces/WorkspaceConfiguration.php
+++ b/workspaces/src/Engines/Workspaces/WorkspaceConfiguration.php
@@ -207,7 +207,7 @@
}
try {
- $authenticationMethod = $class::loadFromArray($authData);
+ $authenticationMethod = $class::loadFromArray($authData, $context);
$authenticationMethod->context = $context;
} catch (Exception $ex) {
throw new WorkspaceException(
diff --git a/workspaces/src/includes/objects/user.php b/workspaces/src/includes/objects/user.php
--- a/workspaces/src/includes/objects/user.php
+++ b/workspaces/src/includes/objects/user.php
@@ -19,16 +19,14 @@
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
use Waystone\Workspaces\Engines\Workspaces\Workspace;
-use Keruald\OmniTools\DataTypes\Option\None;
-use Keruald\OmniTools\DataTypes\Option\Option;
-use Keruald\OmniTools\DataTypes\Option\Some;
+use Keruald\Database\DatabaseEngine;
/**
* User class
*/
class User {
- public $id;
+ public ?int $id;
public $name;
public $password;
public $active = 0;
@@ -39,43 +37,27 @@
public string $lastError;
- /**
- * @var Array An array of users already loaded, the username as user id
- */
- public static $hashtableById = [];
-
/**
* @var array|null An array of the workspaces the user has access to, each element an instance of the Workspace object. As long as the field hasn't been initialized by get_workspaces, null.
*/
private $workspaces = null;
+ private DatabaseEngine $db;
+
/*
* Initializes a new instance
*
* @param int $id the primary key
*/
- function __construct ($id = null) {
+ function __construct ($id = null, DatabaseEngine $db = null) {
+ $this->id = $id;
+ $this->db = $db;
+
if ($id) {
- $this->id = $id;
$this->load_from_database();
}
}
- /**
- * Initializes a new User instance if needed or get already available one.
- *
- * @param iint $id the user ID
- * @return User the user instance
- */
- static function get ($id = NULL) {
- if ($id && array_key_exists($id, User::$hashtableById)) {
- return self::$hashtableById[$id];
- }
-
- $user = new self($id);
- return $user;
- }
-
/**
* Loads the object User (ie fill the properties) from the $_POST array
*/
@@ -92,8 +74,10 @@
* Loads the object User (ie fill the properties) from the database
*/
function load_from_database () {
- global $db;
- $sql = "SELECT * FROM " . TABLE_USERS . " WHERE user_id = '" . $this->id . "'";
+ $db = $this->db;
+
+ $id = $this->db->escape($this->id);
+ $sql = "SELECT * FROM " . TABLE_USERS . " WHERE user_id = '" . $id . "'";
if ( !($result = $db->query($sql)) ) ErrorHandling::messageAndDie(SQL_ERROR, "Unable to query users", '', __LINE__, __FILE__, $sql);
if (!$row = $db->fetchRow($result)) {
$this->lastError = "User unknown: " . $this->id;
@@ -115,24 +99,13 @@
$this->active = $row['user_active'] ? true : false;
$this->email = $row['user_email'];
$this->regdate = $row['user_regdate'];
-
- //Puts object in hashtable, so it's accessible in future call of
- //this run through User::get($id).
- self::$hashtableById[$this->id] = $this;
- }
-
- private static function fromRow (array $row) : User {
- $user = new User();
- $user->load_from_row($row);
-
- return $user;
}
/**
* Saves to database
*/
function save_to_database () {
- global $db;
+ $db = $this->db;
$id = $this->id ? "'" . $db->escape($this->id) . "'" : 'NULL';
$name = $db->escape($this->name);
@@ -157,7 +130,8 @@
* Updates the specified field in the database record
*/
function save_field ($field) {
- global $db;
+ $db = $this->db;
+
if (!$this->id) {
ErrorHandling::messageAndDie(GENERAL_ERROR, "You're trying to update a record not yet saved in the database");
}
@@ -177,7 +151,7 @@
* Generates a unique user id
*/
function generate_id () {
- global $db;
+ $db = $this->db;
do {
$this->id = mt_rand(2001, 9999);
@@ -197,102 +171,23 @@
$this->password = md5($newpassword);
}
- /**
- * Checks if a login is available
- *
- * @param string $login the login to check
- * @return boolean true if the login is available; otherwise, false.
- */
- public static function is_available_login ($login) {
- global $db;
- $sql = "SELECT COUNT(*) FROM " . TABLE_USERS . " WHERE username = '$login'";
- if (!$result = $db->query($sql)) {
- ErrorHandling::messageAndDie(SQL_ERROR, "Can't check if the specified login is available", '', __LINE__, __FILE__, $sql);
- }
- $row = $db->fetchRow($result);
- return ($row[0] == 0);
- }
-
/**
* Initializes a new User instance ready to have its property filled
*
* @return User the new user instance
*/
- public static function create () {
- $user = new User();
+ public static function create (DatabaseEngine $db) {
+ $user = new User(null, $db);
$user->generate_id();
$user->active = true;
$user->regdate = time();
return $user;
}
- /**
- * @return Option<User>
- */
- private static function getByProperty ($property, $value) : Option {
- global $db;
-
- $value = $db->escape($value);
- $sql = "SELECT * FROM " . TABLE_USERS . " WHERE $property = '$value'";
- if (!$result = $db->query($sql)) {
- ErrorHandling::messageAndDie(SQL_ERROR, "Can't get user", '', __LINE__, __FILE__, $sql);
- }
-
- if ($row = $db->fetchRow($result)) {
- return new Some(User::fromRow($row));
- }
-
- return new None;
- }
-
- /**
- * Gets user from specified e-mail
- *
- * @return Option<User> the user matching the specified e-mail; None, if the mail were not found.
- */
- public static function get_user_from_email ($mail) : Option {
- return self::getByProperty("user_email", $mail);
- }
-
- public static function get_user_from_username ($username) : Option {
- return self::getByProperty("username", $username);
- }
-
- public static function resolveUserID ($expression) : Option {
- return self::get_user_from_username($expression)
- ->orElse(self::get_user_from_email($expression))
- ->map(fn($user) => $user->id);
- }
-
//
// REMOTE IDENTITY PROVIDERS
//
- /**
- * Gets user from remote identity provider identifiant
- *
- * @param $authType The authentication method type
- * @param $remoteUserId The remote user identifier
- * @return User the user matching the specified identity provider and identifiant; null if no user were found.
- */
- public static function getUserFromRemoteIdentity ($authType, $remoteUserId) {
- global $db;
-
- $authType = $db->escape($authType);
- $remoteUserId = $db->escape($remoteUserId);
- $sql = "SELECT user_id FROM " . TABLE_USERS_AUTH . " WHERE "
- . "auth_type = '$authType' AND auth_identity = '$remoteUserId'";
- if (!$result = $db->query($sql)) {
- ErrorHandling::messageAndDie(SQL_ERROR, "Can't get user", '', __LINE__, __FILE__, $sql);
- }
-
- if ($row = $db->fetchRow($result)) {
- return User::get($row['user_id']);
- }
-
- return null;
- }
-
/**
* Sets user's remote identity provider identifiant
*
@@ -300,7 +195,8 @@
* @param $remoteUserId The remote user identifier
* */
public function setRemoteIdentity ($authType, $remoteUserId, $properties = null) {
- global $db;
+ $db = $this->db;
+
$authType = $db->escape($authType);
$remoteUserId = $db->escape($remoteUserId);
$properties = ($properties === NULL) ? 'NULL' : "'" . $db->escape($properties) . "'";
@@ -321,7 +217,7 @@
* @return array an array containing group_id, matching groups the current user has access to.
*/
public function get_groups () {
- return self::get_groups_from_user_id($this->id);
+ return self::get_groups_from_user_id($this->id, $this->db);
}
/**
@@ -330,7 +226,8 @@
* @param UserGroup $group The group to check
*/
public function isMemberOfGroup (UserGroup $group) {
- global $db;
+ $db = $this->db;
+
$sql = "SELECT count(*) FROM users_groups_members WHERE group_id = $group->id AND user_id = $this->id";
if (!$result = $db->query($sql)) {
ErrorHandling::messageAndDie(SQL_ERROR, "Can't determine if the user belongs to the group", '', __LINE__, __FILE__, $sql);
@@ -347,7 +244,8 @@
* @parap boolean $isAdmin if true, set the user admin; otherwise, set it regular user.
*/
public function addToGroup (UserGroup $group, $isAdmin = false) {
- global $db;
+ $db = $this->db;
+
$isAdmin = $isAdmin ? 1 : 0;
$sql = "REPLACE INTO users_groups_members VALUES ($group->id, $this->id, $isAdmin)";
if (!$db->query($sql)) {
@@ -361,7 +259,7 @@
* @return string The SQL WHERE clause
*/
public function get_permissions_clause () {
- return self::get_permissions_clause_from_user_id($this->id);
+ return self::get_permissions_clause_from_user_id($this->id, $this->db);
}
/**
@@ -385,7 +283,8 @@
* @param int $permissionFlag The permission flag (facultative; by default, 1)
*/
public function setPermission ($resourceType, $resourceId, $permissionName, $permissionFlag = 1) {
- global $db;
+ $db = $this->db;
+
$resourceType = $db->escape($resourceType);
if (!is_numeric($resourceId)) {
throw new Exception("Resource ID must be a positive or null integer, and not $resourceId.");
@@ -414,8 +313,7 @@
* @param int $user_id the user to get the groups list
* @return array an array containing group_id, matching groups the specified user has access to.
*/
- public static function get_groups_from_user_id ($user_id) {
- global $db;
+ public static function get_groups_from_user_id ($user_id, DatabaseEngine $db) {
$sql = "SELECT group_id FROM " . TABLE_UGROUPS_MEMBERS . " WHERE user_id = " . $user_id;
if (!$result = $db->query($sql)) {
ErrorHandling::messageAndDie(SQL_ERROR, "Can't get user groups", '', __LINE__, __FILE__, $sql);
@@ -433,9 +331,9 @@
* @param $user_id The user ID
* @return string The SQL WHERE clause
*/
- public static function get_permissions_clause_from_user_id ($user_id) {
+ public static function get_permissions_clause_from_user_id ($user_id, DatabaseEngine $db) {
$clause = "subject_resource_type = 'U' AND subject_resource_id = $user_id";
- if ($groups = self::get_groups_from_user_id ($user_id)) {
+ if ($groups = self::get_groups_from_user_id ($user_id, $db)) {
$clause = "($clause) OR (subject_resource_type = 'G' AND subject_resource_id = ";
$clause .= join(") OR (subject_resource_type = 'G' AND subject_resource_id = ", $groups);
$clause .= ')';

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 13, 00:18 (21 h, 15 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3160368
Default Alt Text
D3857.diff (27 KB)

Event Timeline