first commit
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/userGroup/Collector.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\userGroup\Collector
|
||||
*
|
||||
* @brief A helper class to configure a Query Builder to get a collection of userGroups
|
||||
*/
|
||||
|
||||
namespace PKP\userGroup;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\core\interfaces\CollectorInterface;
|
||||
use PKP\plugins\Hook;
|
||||
|
||||
/**
|
||||
* @template T of UserGroup
|
||||
*/
|
||||
class Collector implements CollectorInterface
|
||||
{
|
||||
public const ORDERBY_ROLE_ID = 'roleId';
|
||||
public const ORDERBY_ID = 'id';
|
||||
|
||||
public ?string $orderBy = null;
|
||||
|
||||
/** @var DAO */
|
||||
public $dao;
|
||||
|
||||
public ?array $userGroupIds = null;
|
||||
|
||||
public ?array $contextIds = null; // getByContextId,
|
||||
|
||||
public ?array $roleIds = null;
|
||||
|
||||
public ?array $stageIds = null; // getUserGroupsByStage
|
||||
|
||||
public ?array $publicationIds = null;
|
||||
|
||||
public ?bool $isDefault = null;
|
||||
|
||||
public ?bool $isRecommendOnly = null; // getRecommendOnlyGroupIds
|
||||
|
||||
public ?bool $permitSelfRegistration = null; // getRecommendOnlyGroupIds
|
||||
|
||||
public ?bool $permitMetadataEdit = null;
|
||||
|
||||
public ?bool $showTitle = null; // getPermitMetadataEditGroupIds
|
||||
|
||||
public ?array $userIds = null;
|
||||
|
||||
public ?int $count = null;
|
||||
|
||||
public ?int $offset = null;
|
||||
|
||||
public function __construct(DAO $dao)
|
||||
{
|
||||
$this->dao = $dao;
|
||||
}
|
||||
|
||||
public function getCount(): int
|
||||
{
|
||||
return $this->dao->getCount($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int,int>
|
||||
*/
|
||||
public function getIds(): Collection
|
||||
{
|
||||
return $this->dao->getIds($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc DAO::getMany()
|
||||
* @return LazyCollection<int,T>
|
||||
*/
|
||||
public function getMany(): LazyCollection
|
||||
{
|
||||
return $this->dao->getMany($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by multiple ids
|
||||
*/
|
||||
public function filterByUserGroupIds(?array $ids): self
|
||||
{
|
||||
$this->userGroupIds = $ids;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by context IDs
|
||||
*/
|
||||
public function filterByContextIds(?array $contextIds): self
|
||||
{
|
||||
$this->contextIds = $contextIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by publication IDs
|
||||
*/
|
||||
public function filterByPublicationIds(?array $publicationIds): self
|
||||
{
|
||||
$this->publicationIds = $publicationIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by roles
|
||||
*/
|
||||
public function filterByRoleIds(?array $roleIds): self
|
||||
{
|
||||
$this->roleIds = $roleIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by contexts
|
||||
*/
|
||||
public function filterByStageIds(?array $stageIds): self
|
||||
{
|
||||
$this->stageIds = $stageIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by is default
|
||||
*/
|
||||
public function filterByIsDefault(?bool $isDefault): self
|
||||
{
|
||||
$this->isDefault = $isDefault;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by show title
|
||||
*/
|
||||
public function filterByShowTitle(?bool $showTitle): self
|
||||
{
|
||||
$this->showTitle = $showTitle;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by permit self registration
|
||||
*/
|
||||
public function filterByPermitSelfRegistration(?bool $permitSelfRegistration): self
|
||||
{
|
||||
$this->permitSelfRegistration = $permitSelfRegistration;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by permit metadata edit
|
||||
*/
|
||||
public function filterByPermitMetadataEdit(?bool $permitMetadataEdit): self
|
||||
{
|
||||
$this->permitMetadataEdit = $permitMetadataEdit;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by permit metadata edit
|
||||
*/
|
||||
public function filterByIsRecommendOnly(): self
|
||||
{
|
||||
$this->isRecommendOnly = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by user ids
|
||||
*/
|
||||
public function filterByUserIds(?array $userIds): self
|
||||
{
|
||||
$this->userIds = $userIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Include orderBy columns to the collector query
|
||||
*/
|
||||
public function orderBy(?string $orderBy): self
|
||||
{
|
||||
$this->orderBy = $orderBy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Limit the number of objects retrieved
|
||||
*/
|
||||
public function limit(?int $count): self
|
||||
{
|
||||
$this->count = $count;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset the number of objects retrieved, for example to
|
||||
* retrieve the second page of contents
|
||||
*/
|
||||
public function offset(?int $offset): self
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc CollectorInterface::getQueryBuilder()
|
||||
*/
|
||||
public function getQueryBuilder(): Builder
|
||||
{
|
||||
$q = DB::table('user_groups as ug')
|
||||
->select('ug.*');
|
||||
|
||||
if (isset($this->userGroupIds)) {
|
||||
$q->whereIn('ug.user_group_id', $this->userGroupIds);
|
||||
}
|
||||
|
||||
if (isset($this->userIds)) {
|
||||
$q->join('user_user_groups as uug', 'ug.user_group_id', '=', 'uug.user_group_id');
|
||||
$q->whereIn('uug.user_id', $this->userIds);
|
||||
}
|
||||
|
||||
if (isset($this->publicationIds)) {
|
||||
$q->join('authors as a', 'a.user_group_id', '=', 'ug.user_group_id')
|
||||
->whereIn('a.publication_id', $this->publicationIds);
|
||||
}
|
||||
|
||||
if (isset($this->stageIds)) {
|
||||
$q->join('user_group_stage as ugs', function ($join) {
|
||||
$join->on('ug.user_group_id', '=', 'ugs.user_group_id');
|
||||
$join->on('ug.context_id', '=', 'ugs.context_id');
|
||||
})->whereIn('ugs.stage_id', $this->stageIds);
|
||||
}
|
||||
|
||||
if (isset($this->contextIds)) {
|
||||
$q->whereIn('ug.context_id', $this->contextIds);
|
||||
}
|
||||
|
||||
if (isset($this->roleIds)) {
|
||||
$q->whereIn('ug.role_id', $this->roleIds);
|
||||
}
|
||||
|
||||
$q->when($this->isRecommendOnly !== null, function (Builder $q) {
|
||||
$q->whereIn('ug.user_group_id', function (Builder $q) {
|
||||
$q->select('user_group_id')
|
||||
->from($this->dao->settingsTable)
|
||||
->where('setting_name', '=', 'recommendOnly')
|
||||
->where('setting_value', $this->isRecommendOnly);
|
||||
});
|
||||
});
|
||||
|
||||
$q->when($this->isDefault !== null, function (Builder $q) {
|
||||
$q->where('ug.is_default', $this->isDefault ? 1 : 0);
|
||||
});
|
||||
|
||||
$q->when($this->permitSelfRegistration !== null, function (Builder $q) {
|
||||
$q->where('ug.permit_self_registration', $this->permitSelfRegistration ? 1 : 0);
|
||||
});
|
||||
|
||||
$q->when($this->permitMetadataEdit !== null, function (Builder $q) {
|
||||
$q->where('ug.permit_metadata_edit', $this->permitMetadataEdit ? 1 : 0);
|
||||
});
|
||||
|
||||
$q->when($this->showTitle !== null, function (Builder $q) {
|
||||
$q->where('ug.show_title', $this->showTitle ? 1 : 0);
|
||||
});
|
||||
|
||||
if (isset($this->count)) {
|
||||
$q->limit($this->count);
|
||||
}
|
||||
|
||||
if (isset($this->offset)) {
|
||||
$q->offset($this->offset);
|
||||
}
|
||||
|
||||
switch ($this->orderBy) {
|
||||
case self::ORDERBY_ROLE_ID:
|
||||
$q->orderBy('ug.role_id', 'asc');
|
||||
break;
|
||||
case self::ORDERBY_ID:
|
||||
$q->orderBy('ug.user_group_id', 'asc');
|
||||
break;
|
||||
}
|
||||
|
||||
// Add app-specific query statements
|
||||
Hook::call('UserGroup::Collector', [&$q, $this]);
|
||||
|
||||
return $q;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/userGroup/DAO.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\userGroup\DAO
|
||||
*
|
||||
* @see \PKP\userGroup\UserGroup
|
||||
*
|
||||
* @brief Operations for retrieving and modifying UserGroup objects.
|
||||
*/
|
||||
|
||||
namespace PKP\userGroup;
|
||||
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\core\EntityDAO;
|
||||
use PKP\core\traits\EntityWithParent;
|
||||
use PKP\services\PKPSchemaService;
|
||||
|
||||
/**
|
||||
* @template T of UserGroup
|
||||
* @extends EntityDAO<T>
|
||||
*/
|
||||
class DAO extends EntityDAO
|
||||
{
|
||||
use EntityWithParent;
|
||||
|
||||
/** @copydoc EntityDAO::$schema */
|
||||
public $schema = PKPSchemaService::SCHEMA_USER_GROUP;
|
||||
|
||||
/** @copydoc EntityDAO::$table */
|
||||
public $table = 'user_groups';
|
||||
|
||||
/** @copydoc EntityDAO::$settingsTable */
|
||||
public $settingsTable = 'user_group_settings';
|
||||
|
||||
/** @copydoc EntityDAO::$primaryKeyColumn */
|
||||
public $primaryKeyColumn = 'user_group_id';
|
||||
|
||||
/** @copydoc EntityDAO::$primaryTableColumns */
|
||||
public $primaryTableColumns = [
|
||||
'id' => 'user_group_id',
|
||||
'contextId' => 'context_id',
|
||||
'roleId' => 'role_id',
|
||||
'isDefault' => 'is_default',
|
||||
'showTitle' => 'show_title',
|
||||
'permitSelfRegistration' => 'permit_self_registration',
|
||||
'permitMetadataEdit' => 'permit_metadata_edit',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the parent object ID column name
|
||||
*/
|
||||
public function getParentColumn(): string
|
||||
{
|
||||
return 'context_id';
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiate a new DataObject
|
||||
*/
|
||||
public function newDataObject(): UserGroup
|
||||
{
|
||||
return app(UserGroup::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the total count of rows matching the configured query
|
||||
*/
|
||||
public function getCount(Collector $query): int
|
||||
{
|
||||
return $query
|
||||
->getQueryBuilder()
|
||||
->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of ids matching the configured query
|
||||
*
|
||||
* @return Collection<int,int>
|
||||
*/
|
||||
public function getIds(Collector $query): Collection
|
||||
{
|
||||
return $query
|
||||
->getQueryBuilder()
|
||||
->select('ug.' . $this->primaryKeyColumn)
|
||||
->pluck('ug.' . $this->primaryKeyColumn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection of publications matching the configured query
|
||||
* @return LazyCollection<int,T>
|
||||
*/
|
||||
public function getMany(Collector $query): LazyCollection
|
||||
{
|
||||
$rows = $query
|
||||
->getQueryBuilder()
|
||||
->get();
|
||||
|
||||
return LazyCollection::make(function () use ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
yield $row->user_group_id => $this->fromRow($row);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::fromRow()
|
||||
*/
|
||||
public function fromRow(object $row): UserGroup
|
||||
{
|
||||
$userGroup = parent::fromRow($row);
|
||||
|
||||
return $userGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::insert()
|
||||
*/
|
||||
public function insert(UserGroup $userGroup): int
|
||||
{
|
||||
return parent::_insert($userGroup);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::update()
|
||||
*/
|
||||
public function update(UserGroup $userGroup)
|
||||
{
|
||||
parent::_update($userGroup);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc EntityDAO::delete()
|
||||
*/
|
||||
public function delete(UserGroup $userGroup)
|
||||
{
|
||||
parent::_delete($userGroup);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a keyed Collection (key = user_group_id, value = count) with the amount of active users for each user group
|
||||
*/
|
||||
public function getUserCountByContextId(?int $contextId = null): Collection
|
||||
{
|
||||
return DB::table('user_groups', 'ug')
|
||||
->join('user_user_groups AS uug', 'uug.user_group_id', '=', 'ug.user_group_id')
|
||||
->join('users AS u', 'u.user_id', '=', 'uug.user_id')
|
||||
->when($contextId !== null, fn (Builder $query) => $query->where('ug.context_id', '=', $contextId))
|
||||
->where('u.disabled', '=', 0)
|
||||
->groupBy('ug.user_group_id')
|
||||
->select('ug.user_group_id')
|
||||
->selectRaw('COUNT(0) AS count')
|
||||
->pluck('count', 'user_group_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,475 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/userGroup/Repository.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2000-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\userGroup\Repository
|
||||
*
|
||||
* @brief A repository to find and manage userGroups.
|
||||
*/
|
||||
|
||||
namespace PKP\userGroup;
|
||||
|
||||
use APP\core\Request;
|
||||
use APP\core\Services;
|
||||
use APP\facades\Repo;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\db\DAORegistry;
|
||||
use PKP\plugins\Hook;
|
||||
use PKP\security\Role;
|
||||
use PKP\services\PKPSchemaService;
|
||||
use PKP\site\SiteDAO;
|
||||
use PKP\userGroup\relationships\UserGroupStage;
|
||||
use PKP\userGroup\relationships\UserUserGroup;
|
||||
use PKP\validation\ValidatorFactory;
|
||||
use PKP\xml\PKPXMLParser;
|
||||
|
||||
class Repository
|
||||
{
|
||||
/**
|
||||
* A list of roles not able to change submissionMetadataEdit permission option.
|
||||
*/
|
||||
public const NOT_CHANGE_METADATA_EDIT_PERMISSION_ROLES = [Role::ROLE_ID_MANAGER];
|
||||
|
||||
/** @var DAO */
|
||||
public $dao;
|
||||
|
||||
/** @var string $schemaMap The name of the class to map this entity to its schema */
|
||||
public $schemaMap = maps\Schema::class;
|
||||
|
||||
/** @var Request */
|
||||
protected $request;
|
||||
|
||||
/** @var PKPSchemaService<UserGroup> */
|
||||
protected $schemaService;
|
||||
|
||||
public function __construct(DAO $dao, Request $request, PKPSchemaService $schemaService)
|
||||
{
|
||||
$this->dao = $dao;
|
||||
$this->request = $request;
|
||||
$this->schemaService = $schemaService;
|
||||
}
|
||||
|
||||
/** @copydoc DAO::newDataObject() */
|
||||
public function newDataObject(array $params = []): UserGroup
|
||||
{
|
||||
$object = $this->dao->newDataObject();
|
||||
if (!empty($params)) {
|
||||
$object->setAllData($params);
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
||||
/** @copydoc DAO::get() */
|
||||
public function get(int $id, int $contextId = null): ?UserGroup
|
||||
{
|
||||
return $this->dao->get($id, $contextId);
|
||||
}
|
||||
|
||||
/** @copydoc DAO::exists() */
|
||||
public function exists(int $id, int $contextId = null): bool
|
||||
{
|
||||
return $this->dao->exists($id, $contextId);
|
||||
}
|
||||
|
||||
/** @copydoc DAO::getCollector() */
|
||||
public function getCollector(): Collector
|
||||
{
|
||||
return app(Collector::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the map class for mapping
|
||||
* user groups to their schema
|
||||
*/
|
||||
public function getSchemaMap(): maps\Schema
|
||||
{
|
||||
return app('maps')->withExtensions($this->schemaMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate properties for a user group
|
||||
*
|
||||
* Perform validation checks on data used to add or edit a user group.
|
||||
*
|
||||
* @param UserGroup|null $userGroup The userGroup being edited. Pass `null` if creating a new userGroup
|
||||
* @param array $props A key/value array with the new data to validate
|
||||
* @param array $allowedLocales The context's supported submission locales
|
||||
* @param string $primaryLocale The submission's primary locale
|
||||
*
|
||||
* @return array A key/value array with validation errors. Empty if no errors
|
||||
*/
|
||||
public function validate($userGroup, $props, $allowedLocales, $primaryLocale)
|
||||
{
|
||||
$schemaService = Services::get('schema');
|
||||
|
||||
$validator = ValidatorFactory::make(
|
||||
$props,
|
||||
$schemaService->getValidationRules(PKPSchemaService::SCHEMA_USER_GROUP, $allowedLocales)
|
||||
);
|
||||
|
||||
// Check required fields
|
||||
ValidatorFactory::required(
|
||||
$validator,
|
||||
$userGroup,
|
||||
$schemaService->getRequiredProps(PKPSchemaService::SCHEMA_USER_GROUP),
|
||||
$schemaService->getMultilingualProps(PKPSchemaService::SCHEMA_USER_GROUP),
|
||||
$allowedLocales,
|
||||
$primaryLocale
|
||||
);
|
||||
|
||||
// Check for input from disallowed locales
|
||||
ValidatorFactory::allowedLocales($validator, $schemaService->getMultilingualProps(PKPSchemaService::SCHEMA_USER_GROUP), $allowedLocales);
|
||||
|
||||
$errors = [];
|
||||
if ($validator->fails()) {
|
||||
$errors = $schemaService->formatValidationErrors($validator->errors());
|
||||
}
|
||||
|
||||
Hook::call('UserGroup::validate', [$errors, $userGroup, $props, $allowedLocales, $primaryLocale]);
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
public function add(UserGroup $userGroup): int
|
||||
{
|
||||
$userGroupId = $this->dao->insert($userGroup);
|
||||
$userGroup = Repo::userGroup()->get($userGroupId);
|
||||
|
||||
Hook::call('UserGroup::add', [$userGroup]);
|
||||
|
||||
return $userGroup->getId();
|
||||
}
|
||||
|
||||
public function edit(UserGroup $userGroup, array $params)
|
||||
{
|
||||
$newUserGroup = Repo::userGroup()->newDataObject(array_merge($userGroup->_data, $params));
|
||||
|
||||
Hook::call('UserGroup::edit', [$newUserGroup, $userGroup, $params]);
|
||||
|
||||
$this->dao->update($newUserGroup);
|
||||
|
||||
Repo::userGroup()->get($newUserGroup->getId());
|
||||
}
|
||||
|
||||
public function delete(UserGroup $userGroup)
|
||||
{
|
||||
Hook::call('UserGroup::delete::before', [$userGroup]);
|
||||
|
||||
$this->dao->delete($userGroup);
|
||||
|
||||
Hook::call('UserGroup::delete', [$userGroup]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all user groups assigned to a certain context by contextId
|
||||
*
|
||||
* @param int $contextId
|
||||
*/
|
||||
public function deleteByContextId($contextId)
|
||||
{
|
||||
$userGroupIds = Repo::userGroup()->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->getIds();
|
||||
|
||||
foreach ($userGroupIds as $userGroupId) {
|
||||
$this->dao->deleteById($userGroupId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return all user group ids given a certain role id
|
||||
*
|
||||
* @param int $roleId
|
||||
* @param int|null $contextId
|
||||
*/
|
||||
public function getArrayIdByRoleId($roleId, $contextId = null): array
|
||||
{
|
||||
$collector = Repo::userGroup()->getCollector()
|
||||
->filterByRoleIds([$roleId]);
|
||||
|
||||
if ($contextId) {
|
||||
$collector->filterByContextIds([$contextId]);
|
||||
}
|
||||
|
||||
return $collector->getIds()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all user group ids given a certain role id
|
||||
*
|
||||
* @param ?bool $default Give null for all user groups, else define whether it is default
|
||||
* @return LazyCollection<int,UserGroup>
|
||||
*/
|
||||
public function getByRoleIds(array $roleIds, int $contextId, ?bool $default = null): LazyCollection
|
||||
{
|
||||
$collector = Repo::userGroup()
|
||||
->getCollector()
|
||||
->filterByRoleIds($roleIds)
|
||||
->filterByContextIds([$contextId])
|
||||
->filterByIsDefault($default);
|
||||
|
||||
return $collector->getMany();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all user groups ids for a user id
|
||||
* @return LazyCollection<int,UserGroup>
|
||||
*/
|
||||
public function userUserGroups(int $userId, ?int $contextId = null): LazyCollection
|
||||
{
|
||||
$collector = Repo::userGroup()
|
||||
->getCollector()
|
||||
->filterByUserIds([$userId]);
|
||||
|
||||
if ($contextId) {
|
||||
$collector->filterByContextIds([$contextId]);
|
||||
}
|
||||
|
||||
return $collector->getMany();
|
||||
}
|
||||
|
||||
/**
|
||||
* return whether a user is in a user group
|
||||
*/
|
||||
public function userInGroup(int $userId, int $userGroupId): bool
|
||||
{
|
||||
return UserUserGroup::withUserId($userId)
|
||||
->withUserGroupId($userGroupId)
|
||||
->get()
|
||||
->isNotEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* return whether a context has a specific user group
|
||||
*/
|
||||
public function contextHasGroup(int $contextId, int $userGroupId): bool
|
||||
{
|
||||
return Repo::userGroup()
|
||||
->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->filterByUserGroupIds([$userGroupId])
|
||||
->getCount() > 0;
|
||||
}
|
||||
|
||||
public function assignUserToGroup(int $userId, int $userGroupId): UserUserGroup
|
||||
{
|
||||
return UserUserGroup::create([
|
||||
'userId' => $userId,
|
||||
'userGroupId' => $userGroupId
|
||||
]);
|
||||
}
|
||||
|
||||
public function removeUserFromGroup($userId, $userGroupId, $contextId): bool
|
||||
{
|
||||
return UserUserGroup::withUserId($userId)
|
||||
->withUserGroupId($userGroupId)
|
||||
->withContextId($contextId)
|
||||
->delete();
|
||||
}
|
||||
|
||||
public function deleteAssignmentsByUserId(int $userId, ?int $userGroupId = null): bool
|
||||
{
|
||||
$query = UserUserGroup::withUserId($userId);
|
||||
|
||||
if ($userGroupId) {
|
||||
$query->withUserGroupId($userGroupId);
|
||||
}
|
||||
|
||||
return $query->delete();
|
||||
}
|
||||
|
||||
public function deleteAssignmentsByContextId(int $contextId, ?int $userId = null): bool
|
||||
{
|
||||
$userUserGroups = UserUserGroup::withContextId($contextId);
|
||||
|
||||
if ($userId) {
|
||||
$userUserGroups->withUserId($userId);
|
||||
}
|
||||
|
||||
return $userUserGroups->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user groups assigned to each stage.
|
||||
*
|
||||
* @param null|mixed $roleId
|
||||
* @param null|mixed $count
|
||||
*
|
||||
* @return LazyCollection<int,UserGroup>
|
||||
*/
|
||||
public function getUserGroupsByStage($contextId, $stageId, $roleId = null, $count = null): LazyCollection
|
||||
{
|
||||
$userGroups = $this->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->filterByStageIds([$stageId]);
|
||||
|
||||
if ($roleId) {
|
||||
$userGroups->filterByRoleIds([$roleId]);
|
||||
}
|
||||
|
||||
$userGroups->orderBy(Collector::ORDERBY_ROLE_ID);
|
||||
|
||||
return $userGroups->getMany();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a user group from a stage
|
||||
*/
|
||||
public function removeGroupFromStage(int $contextId, int $userGroupId, int $stageId): bool
|
||||
{
|
||||
return UserGroupStage::withContextId($contextId)
|
||||
->withUserGroupId($userGroupId)
|
||||
->withStageId($stageId)
|
||||
->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all stages assigned to one user group in one context.
|
||||
*
|
||||
* @param int $contextId The context ID.
|
||||
* @param int $userGroupId The user group ID
|
||||
*
|
||||
*/
|
||||
public function getAssignedStagesByUserGroupId(int $contextId, int $userGroupId): Collection
|
||||
{
|
||||
return UserGroupStage::withContextId($contextId)
|
||||
->withUserGroupId($userGroupId)
|
||||
->pluck('stage_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a keyed Collection (key = user_group_id, value = count) with the amount of active users for each user group
|
||||
*/
|
||||
public function getUserCountByContextId(?int $contextId = null): Collection
|
||||
{
|
||||
return $this->dao->getUserCountByContextId($contextId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user group a new author may be assigned to
|
||||
* when they make their first submission, if they are
|
||||
* not already assigned to an author user group.
|
||||
*
|
||||
* This returns the first user group with ROLE_ID_AUTHOR
|
||||
* that permits self-registration.
|
||||
*/
|
||||
public function getFirstSubmitAsAuthorUserGroup(int $contextId): ?UserGroup
|
||||
{
|
||||
return Repo::userGroup()
|
||||
->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->filterByRoleIds([Role::ROLE_ID_AUTHOR])
|
||||
->filterByPermitSelfRegistration(true)
|
||||
->limit(1)
|
||||
->getMany()
|
||||
->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the XML file and move the settings to the DB
|
||||
*
|
||||
* @param int $contextId
|
||||
* @param string $filename
|
||||
*
|
||||
* @return bool true === success
|
||||
*/
|
||||
public function installSettings($contextId, $filename)
|
||||
{
|
||||
$xmlParser = new PKPXMLParser();
|
||||
$tree = $xmlParser->parse($filename);
|
||||
|
||||
$siteDao = DAORegistry::getDAO('SiteDAO'); /** @var SiteDAO $siteDao */
|
||||
$site = $siteDao->getSite();
|
||||
$installedLocales = $site->getInstalledLocales();
|
||||
|
||||
if (!$tree) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($tree->getChildren() as $setting) {
|
||||
$roleId = hexdec($setting->getAttribute('roleId'));
|
||||
$nameKey = $setting->getAttribute('name');
|
||||
$abbrevKey = $setting->getAttribute('abbrev');
|
||||
$permitSelfRegistration = $setting->getAttribute('permitSelfRegistration');
|
||||
$permitMetadataEdit = $setting->getAttribute('permitMetadataEdit');
|
||||
|
||||
// If has manager role then permitMetadataEdit can't be overridden
|
||||
if (in_array($roleId, [Role::ROLE_ID_MANAGER])) {
|
||||
$permitMetadataEdit = $setting->getAttribute('permitMetadataEdit');
|
||||
}
|
||||
|
||||
$defaultStages = explode(',', (string) $setting->getAttribute('stages'));
|
||||
|
||||
// create a role associated with this user group
|
||||
$userGroup = $this->newDataObject();
|
||||
$userGroup->setRoleId($roleId);
|
||||
$userGroup->setContextId($contextId);
|
||||
$userGroup->setPermitSelfRegistration($permitSelfRegistration ?? false);
|
||||
$userGroup->setPermitMetadataEdit($permitMetadataEdit ?? false);
|
||||
$userGroup->setDefault(true);
|
||||
$userGroup->setShowTitle(true);
|
||||
|
||||
// insert the group into the DB
|
||||
$userGroupId = $this->add($userGroup);
|
||||
|
||||
// Install default groups for each stage
|
||||
if (is_array($defaultStages)) { // test for groups with no stage assignments
|
||||
foreach ($defaultStages as $stageId) {
|
||||
if (!empty($stageId) && $stageId <= WORKFLOW_STAGE_ID_PRODUCTION && $stageId >= WORKFLOW_STAGE_ID_SUBMISSION) {
|
||||
UserGroupStage::create([
|
||||
'contextId' => $contextId,
|
||||
'userGroupId' => $userGroupId,
|
||||
'stageId' => $stageId
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add the i18n keys to the settings table so that they
|
||||
// can be used when a new locale is added/reloaded
|
||||
$newUserGroup = $this->get($userGroupId);
|
||||
$this->edit($newUserGroup, [
|
||||
'nameLocaleKey' => $nameKey,
|
||||
'abbrevLocaleKey' => $abbrevKey
|
||||
]);
|
||||
|
||||
// install the settings in the current locale for this context
|
||||
foreach ($installedLocales as $locale) {
|
||||
$this->installLocale($locale, $contextId);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* use the locale keys stored in the settings table to install the locale settings
|
||||
*
|
||||
* @param string $locale
|
||||
* @param ?int $contextId
|
||||
*/
|
||||
public function installLocale($locale, ?int $contextId = null)
|
||||
{
|
||||
$userGroupsCollector = $this->getCollector();
|
||||
|
||||
if (isset($contextId)) {
|
||||
$userGroupsCollector->filterByContextIds([$contextId]);
|
||||
}
|
||||
|
||||
$userGroups = $userGroupsCollector->getMany();
|
||||
|
||||
foreach ($userGroups as $userGroup) {
|
||||
$nameKey = $userGroup->getData('nameLocaleKey');
|
||||
$userGroup->setData('name', __($nameKey, [], $locale), $locale);
|
||||
$abbrevKey = $userGroup->getData('abbrevLocaleKey');
|
||||
$userGroup->setData('abbrev', __($abbrevKey, [], $locale), $locale);
|
||||
|
||||
$this->edit($userGroup, []);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/userGroup/UserGroup.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\userGroup\UserGroup
|
||||
*
|
||||
* @see DAO
|
||||
*
|
||||
* @brief UserGroup metadata class.
|
||||
*/
|
||||
|
||||
namespace PKP\userGroup;
|
||||
|
||||
class UserGroup extends \PKP\core\DataObject
|
||||
{
|
||||
/**
|
||||
* Get the role ID
|
||||
*
|
||||
* @return int ROLE_ID_...
|
||||
*/
|
||||
public function getRoleId()
|
||||
{
|
||||
return $this->getData('roleId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the role ID
|
||||
*
|
||||
* @param int $roleId ROLE_ID_...
|
||||
*/
|
||||
public function setRoleId($roleId)
|
||||
{
|
||||
$this->setData('roleId', $roleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the role path
|
||||
*
|
||||
* @return string Role path
|
||||
*/
|
||||
public function getPath()
|
||||
{
|
||||
return $this->getData('path');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the role path
|
||||
* $param $path string
|
||||
*/
|
||||
public function setPath($path)
|
||||
{
|
||||
$this->setData('path', $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the context ID
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getContextId()
|
||||
{
|
||||
return $this->getData('contextId');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the context ID
|
||||
*
|
||||
* @param int $contextId
|
||||
*/
|
||||
public function setContextId($contextId)
|
||||
{
|
||||
$this->setData('contextId', $contextId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getDefault()
|
||||
{
|
||||
return $this->getData('isDefault');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default flag
|
||||
*
|
||||
* @param bool $isDefault
|
||||
*/
|
||||
public function setDefault($isDefault)
|
||||
{
|
||||
$this->setData('isDefault', $isDefault);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "show title" flag (whether or not the title of the role
|
||||
* should be included in the list of submission contributor names)
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getShowTitle()
|
||||
{
|
||||
return $this->getData('showTitle');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "show title" flag
|
||||
*
|
||||
* @param bool $showTitle
|
||||
*/
|
||||
public function setShowTitle($showTitle)
|
||||
{
|
||||
$this->setData('showTitle', $showTitle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the "permit self-registration" flag (whether or not users may
|
||||
* self-register for this role, i.e. in the case of external
|
||||
* reviewers, or whether it should be prohibited, in the case of
|
||||
* internal reviewers).
|
||||
*
|
||||
* @return bool True IFF user self-registration is permitted
|
||||
*/
|
||||
public function getPermitSelfRegistration()
|
||||
{
|
||||
return $this->getData('permitSelfRegistration');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "permit self-registration" flag
|
||||
*/
|
||||
public function setPermitSelfRegistration(bool $permitSelfRegistration)
|
||||
{
|
||||
$this->setData('permitSelfRegistration', $permitSelfRegistration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the recommendOnly option (whether or not the manager or the sub-editor role
|
||||
* can only recommend or also make decisions in the submission review)
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getRecommendOnly()
|
||||
{
|
||||
return $this->getData('recommendOnly');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the recommendOnly option (whether or not the manager or the sub-editor role
|
||||
* can only recommend or also make decisions in the submission review)
|
||||
*
|
||||
* @param bool $recommendOnly
|
||||
*/
|
||||
public function setRecommendOnly($recommendOnly)
|
||||
{
|
||||
$this->setData('recommendOnly', $recommendOnly);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the localized role name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalizedName()
|
||||
{
|
||||
return $this->getLocalizedData('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get localized user group name, or array of localized names if $locale is null
|
||||
*
|
||||
* @param string|null $locale
|
||||
*
|
||||
* @return string|array|null localized name or array of localized names or null
|
||||
*/
|
||||
public function getName($locale)
|
||||
{
|
||||
return $this->getData('name', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user group name
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $locale
|
||||
*/
|
||||
public function setName($name, $locale)
|
||||
{
|
||||
$this->setData('name', $name, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the localized abbreviation
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLocalizedAbbrev()
|
||||
{
|
||||
return $this->getLocalizedData('abbrev');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get localized user group abbreviation, or array of localized abbreviations if $locale is null
|
||||
*
|
||||
* @param string|null $locale
|
||||
*
|
||||
* @return string|array|null localized abbreviation or array of localized abbreviations or null
|
||||
*/
|
||||
public function getAbbrev($locale)
|
||||
{
|
||||
return $this->getData('abbrev', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user group abbreviation
|
||||
*
|
||||
* @param string $abbrev
|
||||
* @param string $locale
|
||||
*/
|
||||
public function setAbbrev($abbrev, $locale)
|
||||
{
|
||||
$this->setData('abbrev', $abbrev, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for permitMetadataEdit attribute.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function getPermitMetadataEdit()
|
||||
{
|
||||
return $this->getData('permitMetadataEdit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for permitMetadataEdit attribute.
|
||||
*/
|
||||
public function setPermitMetadataEdit(bool $permitMetadataEdit)
|
||||
{
|
||||
$this->setData('permitMetadataEdit', $permitMetadataEdit);
|
||||
}
|
||||
}
|
||||
|
||||
if (!PKP_STRICT_MODE) {
|
||||
class_alias('\PKP\userGroup\UserGroup', '\UserGroup');
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/userGroup/maps/Schema.php
|
||||
*
|
||||
* Copyright (c) 2014-2020 Simon Fraser University
|
||||
* Copyright (c) 2000-2020 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\userGroup\maps\Schema
|
||||
*
|
||||
* @brief Map userGroups to the properties defined in the userGroup schema
|
||||
*/
|
||||
|
||||
namespace PKP\userGroup\maps;
|
||||
|
||||
use Illuminate\Support\Enumerable;
|
||||
use PKP\core\PKPRequest;
|
||||
use PKP\services\PKPSchemaService;
|
||||
use PKP\userGroup\UserGroup;
|
||||
|
||||
class Schema extends \PKP\core\maps\Schema
|
||||
{
|
||||
public Enumerable $collection;
|
||||
|
||||
public string $schema = PKPSchemaService::SCHEMA_USER_GROUP;
|
||||
|
||||
public function __construct(PKPRequest $request, \PKP\context\Context $context, PKPSchemaService $schemaService)
|
||||
{
|
||||
parent::__construct($request, $context, $schemaService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map an author
|
||||
*
|
||||
* Includes all properties in the announcement schema.
|
||||
*/
|
||||
public function map(UserGroup $item): array
|
||||
{
|
||||
return $this->mapByProperties($this->getProps(), $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize an author
|
||||
*
|
||||
* Includes properties with the apiSummary flag in the author schema.
|
||||
*/
|
||||
public function summarize(UserGroup $item): array
|
||||
{
|
||||
return $this->mapByProperties($this->getSummaryProps(), $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a collection of Authors
|
||||
*
|
||||
* @see self::map
|
||||
*/
|
||||
public function mapMany(Enumerable $collection): Enumerable
|
||||
{
|
||||
$this->collection = $collection;
|
||||
return $collection->map(function ($item) {
|
||||
return $this->map($item);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize a collection of Authors
|
||||
*
|
||||
* @see self::summarize
|
||||
*/
|
||||
public function summarizeMany(Enumerable $collection): Enumerable
|
||||
{
|
||||
$this->collection = $collection;
|
||||
return $collection->map(function ($item) {
|
||||
return $this->summarize($item);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Map schema properties of an Author to an assoc array
|
||||
*/
|
||||
protected function mapByProperties(array $props, UserGroup $item): array
|
||||
{
|
||||
$output = [];
|
||||
foreach ($props as $prop) {
|
||||
switch ($prop) {
|
||||
default:
|
||||
$output[$prop] = $item->getData($prop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->context->getSupportedSubmissionLocales());
|
||||
|
||||
ksort($output);
|
||||
|
||||
return $this->withExtensions($output, $item);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/userGroup/relationships/UserGroupStage.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\userGroup\relationships\UserGroupStage
|
||||
*
|
||||
* @brief UserGroupStage relationship metadata class.
|
||||
*/
|
||||
|
||||
namespace PKP\userGroup\relationships;
|
||||
|
||||
use APP\facades\Repo;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
class UserGroupStage extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
public $incrementing = false;
|
||||
protected $primaryKey = null;
|
||||
protected $fillable = ['userGroupId', 'stageId', 'contextId'];
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'user_group_stage';
|
||||
|
||||
public function userGroup(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value, $attributes) => Repo::userGroup()->get($attributes['user_group_id']),
|
||||
set: fn ($value) => $value->getId()
|
||||
);
|
||||
}
|
||||
|
||||
public function stageId(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value, $attributes) => $attributes['stage_id'],
|
||||
set: fn ($value) => ['stage_id' => $value]
|
||||
);
|
||||
}
|
||||
|
||||
public function userGroupId(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($userGroup, $attributes) => $attributes['user_group_id'],
|
||||
set: fn ($value) => ['user_group_id' => $value]
|
||||
);
|
||||
}
|
||||
|
||||
public function contextId(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value, $attributes) => $attributes['context_id'],
|
||||
set: fn ($value) => ['context_id' => $value]
|
||||
);
|
||||
}
|
||||
|
||||
public function scopeWithStageId(Builder $query, int $stageId): Builder
|
||||
{
|
||||
return $query->where('stage_id', $stageId);
|
||||
}
|
||||
|
||||
public function scopeWithUserGroupId(Builder $query, int $userGroupId): Builder
|
||||
{
|
||||
return $query->where('user_group_id', $userGroupId);
|
||||
}
|
||||
|
||||
public function scopeWithContextId(Builder $query, int $contextId): Builder
|
||||
{
|
||||
return $query->where('context_id', $contextId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file classes/userGroup/relationships/UserUserGroup.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2000-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class \PKP\userGroup\relationships\UserUserGroup
|
||||
*
|
||||
* @brief UserUserGroup metadata class.
|
||||
*/
|
||||
|
||||
namespace PKP\userGroup\relationships;
|
||||
|
||||
use APP\facades\Repo;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
|
||||
class UserUserGroup extends \Illuminate\Database\Eloquent\Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
public $incrementing = false;
|
||||
protected $primaryKey = null;
|
||||
protected $fillable = ['userGroupId', 'userId'];
|
||||
|
||||
public function user(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value, $attributes) => Repo::user()->get($attributes['user_id']),
|
||||
set: fn ($value) => $value->getId()
|
||||
);
|
||||
}
|
||||
|
||||
public function userGroup(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value, $attributes) => Repo::userGroup()->get($attributes['user_group_id']),
|
||||
set: fn ($value) => $value->getId()
|
||||
);
|
||||
}
|
||||
|
||||
public function userId(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($user, $attributes) => $attributes['user_id'],
|
||||
set: fn ($value) => ['user_id' => $value]
|
||||
);
|
||||
}
|
||||
|
||||
public function userGroupId(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($userGroup, $attributes) => $attributes['user_group_id'],
|
||||
set: fn ($value) => ['user_group_id' => $value]
|
||||
);
|
||||
}
|
||||
|
||||
public function scopeWithUserId(Builder $query, int $userId): Builder
|
||||
{
|
||||
return $query->where('user_user_groups.user_id', $userId);
|
||||
}
|
||||
|
||||
public function scopeWithUserGroupId(Builder $query, int $userGroupId): Builder
|
||||
{
|
||||
return $query->where('user_user_groups.user_group_id', $userGroupId);
|
||||
}
|
||||
|
||||
public function scopeWithContextId(Builder $query, int $contextId): Builder
|
||||
{
|
||||
return $query
|
||||
->join('user_groups as ug', 'user_user_groups.user_group_id', '=', 'ug.user_group_id')
|
||||
->where('ug.context_id', $contextId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user