Page MenuHomeDevCentral

No OneTemporary

diff --git a/workspaces/src/Engines/Auth/AuthenticationMethod.php b/workspaces/src/Engines/Auth/AuthenticationMethod.php
index f883431..4908408 100644
--- a/workspaces/src/Engines/Auth/AuthenticationMethod.php
+++ b/workspaces/src/Engines/Auth/AuthenticationMethod.php
@@ -1,290 +1,290 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Authentication method class
*
* @package ObsidianWorkspaces
* @subpackage Auth
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
namespace Waystone\Workspaces\Engines\Auth;
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\ArrayDeserializableWithContext;
use Waystone\Workspaces\Engines\Users\User;
use Keruald\OmniTools\DataTypes\Option\None;
use Keruald\OmniTools\DataTypes\Option\Option;
use Keruald\OmniTools\DataTypes\Option\Some;
use Language;
use Message;
use Exception;
use InvalidArgumentException;
/**
* Authentication method class
*
* This class has to be extended to implement custom authentication methods.
*/
abstract class AuthenticationMethod implements ArrayDeserializableWithContext {
/**
* @var User The local user matching the authentication
*/
public $localUser;
/**
* @var string The username
*/
public $name;
/**
* @var string The e-mail address
*/
public $email;
/**
* @var string The authentication method identifiant
*/
public $id;
/**
* @var string The remote identity provider user identifiant
*/
public $remoteUserId;
/**
* @var Message The localized authentication login message
*/
public $loginMessage;
/**
* @var boolean Determines if the authentication method could be used to
* register new users
*/
public $canCreateUser = false;
/**
* @var Array Actions to execute if a user is created, each instance a
* member of UserAction
*/
public $createUserActions = [];
/**
* @var Context The site context
*/
public $context;
/**
* @var Message The localized authentication error message
*/
public $loginError;
/**
* Gets authentication link for this method
*/
public abstract function getAuthenticationLink ();
/**
* Handles request
*/
public abstract function handleRequest ();
/**
* Runs actions planned on user create
*/
protected function runCreateUserActions () {
foreach ($this->createUserActions as $action) {
$action->context = $this->context;
$action->targetUser = $this->localUser;
$action->run();
}
}
/**
* Finds user from available data
*
* @return Option<User> the user if a user has been found; otherwise, false.
*/
private function findUser () : Option {
- $users = $this->context->userRepository;
+ $users = $this->context->resources->users;
if ($this->remoteUserId != '') {
$user = $users->getUserFromRemoteIdentity(
$this->id, $this->remoteUserId,
);
if ($user->isSome()) {
return $user;
}
}
if ($this->email != '') {
$user = $users->getUserFromEmail($this->email);
if ($user->isSome()) {
return $user;
}
}
return new None;
}
/**
* Signs in or creates a new user
*
* @return boolean true if user has been successfully logged in; otherwise,
* false.
*/
public function signInOrCreateUser () {
// At this stage, if we don't already have a user instance,
// we're fetching it by remote user id or mail.
//
// If no result is returned, we're creating a new user if needed.
//
// Finally, we proceed to log in.
if ($this->localUser === null) {
$user = $this->findUser();
if ($user->isSome()) {
$this->localUser = $user->getValue();
}
}
if ($this->localUser === null) {
if (!$this->canCreateUser) {
$this->loginError =
Language::get("ExternalLoginCantCreateAccount");
return false;
} else {
$this->createUser();
if ($this->localUser === null) {
throw new Exception("Can't sign in: after correct remote authentication, an error occurred creating locally a new user.");
}
}
}
$this->signIn($this->localUser);
return true;
}
/**
* Signs in the specified user
*
* @param User The user to log in
*/
public function signIn (User $user) {
$this->context->session->user_login($user->id);
}
/**
* Creates a new user based on the authentication provisioning information
*
* @return User The user created
*/
public function createUser () {
if (!$this->canCreateUser) {
throw new Exception("Can't create user: the canCreateUser property is set at false.");
}
- $users = $this->context->userRepository;
+ $users = $this->context->resources->users;
$user = $users->create();
$user->name = $this->name;
$user->email = $this->email;
$users->saveToDatabase($user);
$users->setRemoteIdentity(
$user, $this->id, $this->remoteUserId,
);
$this->localUser = $user;
$this->runCreateUserActions();
}
/**
* Gets authentication method from ID
*
* @param string $id The authentication method id
* @param Context $context The site context
*
* @return AuthenticationMethod The authentication method matching the id
*/
public static function getFromId ($id, $context) {
if ($context->workspace != null) {
foreach (
$context->workspace->configuration->authenticationMethods as
$authenticationMethod
) {
if ($authenticationMethod->id == $id) {
return $authenticationMethod;
}
}
}
return null;
}
/**
* Loads an AuthenticationMethod instance from a generic array.
* 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, mixed $context) : self {
$instance = new static;
$instance->context = $context;
if (!array_key_exists("id", $data)) {
throw new InvalidArgumentException("Authentication method id is required.");
}
$instance->id = $data["id"];
$message = $data["loginMessage"] ?? Language::get("SignIn");
$instance->loginMessage = new Message($message);
if (array_key_exists("createUser", $data)) {
$createUser = $data["createUser"];
if (array_key_exists("enabled", $createUser)) {
$instance->canCreateUser = ($createUser["enabled"] === true);
}
$addToGroups = $createUser["addToGroups"] ?? [];
foreach ($addToGroups as $actionData) {
$instance->createUserActions[] =
AddToGroupUserAction::loadFromArray($actionData);
}
$givePermissions = $createUser["givePermissions"] ?? [];
foreach ($createUser["givePermissions"] as $actionData) {
$instance->createUserActions[] =
GivePermissionUserAction::loadFromArray($actionData);
}
}
return $instance;
}
}
diff --git a/workspaces/src/Engines/Framework/Application.php b/workspaces/src/Engines/Framework/Application.php
index 99f4616..94a8c3d 100644
--- a/workspaces/src/Engines/Framework/Application.php
+++ b/workspaces/src/Engines/Framework/Application.php
@@ -1,35 +1,34 @@
<?php
namespace Waystone\Workspaces\Engines\Framework;
use Keruald\Database\Database;
use Waystone\Workspaces\Engines\Errors\ErrorHandling;
use Waystone\Workspaces\Engines\Users\UserRepository;
class Application {
public static function init () : void {
Environment::init();
ErrorHandling::init();
}
public static function getContext(array $config) : Context {
$context = new Context();
$context->config = $config;
$context->db = Database::load($config["sql"]);
- $context->userRepository = new UserRepository($context->db);
$context->resources = new Resources(
- $context->userRepository,
+ new UserRepository($context->db),
);
$context->session = Session::load(
$context->db,
- $context->userRepository,
+ $context->resources->users,
);
$context->url = get_current_url_fragments();
$context->initializeTemplateEngine($context->config['Theme']);
return $context;
}
}
diff --git a/workspaces/src/Engines/Framework/Context.php b/workspaces/src/Engines/Framework/Context.php
index 3c507a1..f6d00b1 100644
--- a/workspaces/src/Engines/Framework/Context.php
+++ b/workspaces/src/Engines/Framework/Context.php
@@ -1,113 +1,108 @@
<?php
/**
* _, __, _, _ __, _ _, _, _
* / \ |_) (_ | | \ | /_\ |\ |
* \ / |_) , ) | |_/ | | | | \|
* ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
*
* Application context class
*
* @package ObsidianWorkspaces
* @subpackage Controller
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
namespace Waystone\Workspaces\Engines\Framework;
use Keruald\Database\DatabaseEngine;
use Smarty\Smarty;
use Waystone\Workspaces\Engines\Users\User;
use Waystone\Workspaces\Engines\Users\UserRepository;
use Waystone\Workspaces\Engines\Workspaces\WorkSpace;
/**
* Context class
*
* This class describes the site context.
*/
class Context {
/**
* @var ?WorkSpace the workspace currently enabled
*/
public ?WorkSpace $workspace = null;
/**
* @var DatabaseEngine the database
*/
public DatabaseEngine $db;
/**
* @var array the configuration
*/
public array $config;
- /**
- * @var UserRepository the users already loaded from database
- */
- public UserRepository $userRepository;
-
public Resources $resources;
/**
* @var ?User the user currently logged in
*/
public ?User $user = null;
/**
* @var Session the current session
*/
public Session $session;
/**
* @var string[] the URL fragments
*/
public array $url;
/**
* @var Smarty the template engine
*/
public Smarty $templateEngine;
///
/// Helper methods
///
/**
* Gets application root directory
*
* @return string|false the application root directory
*/
public function getApplicationRootDirectory () : string|false {
return getcwd();
}
///
/// Templates
///
/**
* Initializes the template engine
*
* @param string $theme the theme for the templates
*/
public function initializeTemplateEngine (string $theme) : void {
$smarty = new Smarty();
$current_dir = static::getApplicationRootDirectory();
$smarty
->setTemplateDir("$current_dir/skins/$theme")
->setCacheDir($this->config["Content"]["Cache"])
->setCompileDir($this->config["Content"]["Cache"] . "/compiled")
->setConfigDir($current_dir);
$smarty->config_vars += [
"StaticContentURL" => $this->config["StaticContentURL"],
];
$this->templateEngine = $smarty;
}
}
diff --git a/workspaces/src/Engines/Framework/Resources.php b/workspaces/src/Engines/Framework/Resources.php
index ec51b63..ec9c049 100644
--- a/workspaces/src/Engines/Framework/Resources.php
+++ b/workspaces/src/Engines/Framework/Resources.php
@@ -1,55 +1,55 @@
<?php
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 UserGroup;
use InvalidArgumentException;
class Resources {
public function __construct (
- private UserRepository $users,
+ public UserRepository $users,
) {
}
/**
* @return Option<int>
*/
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);
}
if (!$identifier) {
return new None;
}
//Searches identifier
switch ($resource_type) {
case 'U':
return $this->users->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);
}
}
}

File Metadata

Mime Type
text/x-diff
Expires
Wed, Mar 18, 12:55 (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3539877
Default Alt Text
(14 KB)

Event Timeline