Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12439133
D3846.id9960.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Referenced Files
None
Subscribers
None
D3846.id9960.diff
View Options
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
@@ -20,6 +20,7 @@
use Waystone\Workspaces\Engines\Auth\Permission;
use Waystone\Workspaces\Engines\Auth\UserAction;
+use Waystone\Workspaces\Engines\Framework\Resources;
use Waystone\Workspaces\Engines\Serialization\ArrayDeserializable;
use Exception;
@@ -56,13 +57,15 @@
* Executes the user action
*/
public function run () {
- if (!$id = resolve_resource_id($this->resourceType,
- $this->resourceIdentifier)) {
- throw new Exception("Can't get identifier from resource "
+ $id = Resources::resolve_id($this->resourceType, $this->resourceIdentifier);
+
+ if ($id->isNone()) {
+ throw new Exception("Can't resolve resource "
. $this->resourceType . " " . $this->resourceIdentifier);
}
+
$this->targetUser->setPermission(
- $this->resourceType, $id,
+ $this->resourceType, $id->getValue(),
$this->permissionName, $this->permissionFlag,
);
}
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
@@ -22,6 +22,10 @@
use Waystone\Workspaces\Engines\Framework\Context;
use Waystone\Workspaces\Engines\Serialization\ArrayDeserializable;
+use Keruald\OmniTools\DataTypes\Option\None;
+use Keruald\OmniTools\DataTypes\Option\Option;
+use Keruald\OmniTools\DataTypes\Option\Some;
+
use Language;
use Message;
use User;
@@ -111,27 +115,27 @@
/**
* Finds user from available data
*
- * @return User the user if a user has been found; otherwise, false.
+ * @return Option<User> the user if a user has been found; otherwise, false.
*/
- private function findUser () {
+ private function findUser () : Option {
if ($this->remoteUserId != '') {
$user = User::getUserFromRemoteIdentity(
$this->id, $this->remoteUserId,
);
if ($user !== null) {
- return $user;
+ return new Some($user);
}
}
if ($this->email != '') {
$user = User::get_user_from_email($this->email);
- if ($user !== null) {
+ if ($user->isSome()) {
return $user;
}
}
- return null;
+ return new None;
}
/**
@@ -149,7 +153,11 @@
// Finally, we proceed to log in.
if ($this->localUser === null) {
- $this->localUser = $this->findUser();
+ $user = $this->findUser();
+
+ if ($user->isSome()) {
+ $this->localUser = $user->getValue();
+ }
}
if ($this->localUser === null) {
diff --git a/workspaces/src/Engines/Framework/Resources.php b/workspaces/src/Engines/Framework/Resources.php
new file mode 100644
--- /dev/null
+++ b/workspaces/src/Engines/Framework/Resources.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Waystone\Workspaces\Engines\Framework;
+
+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 {
+
+ /**
+ * @return Option<int>
+ */
+ public static function resolve_id (string $resource_type, string $identifier) : Option {
+ //Trivial cases: already an ID, null or void ID
+ if (is_numeric($identifier)) {
+ return new Some((int)$identifier);
+ }
+
+ if (!$identifier) {
+ return new None;
+ }
+
+ //Searches identifier
+ switch ($resource_type) {
+ case 'U':
+ return User::resolveUserID($identifier);
+
+ case 'G':
+ $group = UserGroup::fromCode($identifier);
+
+ return new Some($group->id);
+
+ case 'W':
+ $workspace = Workspace::fromCode($identifier);
+
+ return new Some($workspace->id);
+
+ default:
+ throw new InvalidArgumentException("Unknown resource type: $resource_type", E_USER_ERROR);
+ }
+ }
+}
diff --git a/workspaces/src/includes/GlobalFunctions.php b/workspaces/src/includes/GlobalFunctions.php
--- a/workspaces/src/includes/GlobalFunctions.php
+++ b/workspaces/src/includes/GlobalFunctions.php
@@ -2,46 +2,6 @@
use Waystone\Workspaces\Engines\Workspaces\Workspace;
-////////////////////////////////////////////////////////////////////////////////
-/// ///
-/// Information helper functions ///
-/// ///
-////////////////////////////////////////////////////////////////////////////////
-
-/**
- * Gets the resource ID from an identifier
- *
- * @param $resource_type the resource type
- * @param $identifier resource identifier
- * @return mixed the resource ID (as integer), or NULL if unknown
- */
-function resolve_resource_id ($resource_type, $identifier) {
- //Trivial cases: already an ID, null or void ID
- if (is_numeric($identifier)) {
- return $identifier;
- }
- if (!$identifier) {
- return NULL;
- }
-
- //Searches identifier
- switch ($resource_type) {
- case 'U':
- return get_user_id($identifier);
-
- case 'G':
- $group = UserGroup::fromCode($identifier);
- return $group->id;
-
- case 'W':
- $workspace = Workspace::fromCode($identifier);
- return $workspace->id;
-
- default:
- throw new Exception("Unknown resource type: $resource_type", E_USER_ERROR);
- }
-}
-
////////////////////////////////////////////////////////////////////////////////
/// ///
/// Misc helper functions ///
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,6 +19,10 @@
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;
+
/**
* User class
*/
@@ -117,6 +121,13 @@
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
*/
@@ -216,26 +227,41 @@
}
/**
- * Gets user from specified e-mail
- *
- * @return User the user matching the specified e-mail; null, if the mail were not found.
+ * @return Option<User>
*/
- public static function get_user_from_email ($mail) {
+ private static function getByProperty ($property, $value) : Option {
global $db;
- $sql = "SELECT * FROM " . TABLE_USERS . " WHERE user_email = '$mail'";
+
+ $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)) {
- //E-mail found.
- $user = new User();
- $user->load_from_row($row);
- return $user;
+ return new Some(User::fromRow($row));
}
- //E-mail not found.
- return null;
+ 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);
}
//
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Nov 7, 08:11 (21 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3142463
Default Alt Text
D3846.id9960.diff (8 KB)
Attached To
Mode
D3846: Move resolve_resource_id into Resources helper class
Attached
Detach File
Event Timeline
Log In to Comment