first commit
This commit is contained in:
@@ -0,0 +1,406 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/user/form/UserDetailsForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserDetailsForm
|
||||
*
|
||||
* @ingroup controllers_grid_settings_user_form
|
||||
*
|
||||
* @brief Form for editing user profiles.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\user\form;
|
||||
|
||||
use APP\author\Author;
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use APP\notification\NotificationManager;
|
||||
use APP\template\TemplateManager;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use PKP\core\Core;
|
||||
use PKP\core\PKPRequest;
|
||||
use PKP\core\PKPString;
|
||||
use PKP\facades\Locale;
|
||||
use PKP\identity\Identity;
|
||||
use PKP\mail\mailables\UserCreated;
|
||||
use PKP\notification\PKPNotification;
|
||||
use PKP\security\Validation;
|
||||
use PKP\session\SessionManager;
|
||||
use PKP\user\InterestManager;
|
||||
use PKP\user\User;
|
||||
use Symfony\Component\Mailer\Exception\TransportException;
|
||||
|
||||
class UserDetailsForm extends UserForm
|
||||
{
|
||||
/** @var User */
|
||||
public $user;
|
||||
|
||||
/** @var Author An optional author to base this user on */
|
||||
public $author;
|
||||
|
||||
/** @var bool An internal use flag that allows to determine the update only for user group */
|
||||
protected bool $userGroupUpdateOnly = false;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param PKPRequest $request
|
||||
* @param int $userId optional
|
||||
* @param Author $author optional
|
||||
*/
|
||||
public function __construct($request, $userId = null, $author = null)
|
||||
{
|
||||
parent::__construct('controllers/grid/settings/user/form/userDetailsForm.tpl', $userId);
|
||||
|
||||
if (isset($author)) {
|
||||
$this->author = & $author;
|
||||
} else {
|
||||
$this->author = null;
|
||||
}
|
||||
|
||||
// the users register for the site, thus
|
||||
// the site primary locale is the required default locale
|
||||
$this->addSupportedFormLocale($request->getSite()->getPrimaryLocale());
|
||||
|
||||
if ($userId !== null) {
|
||||
$this->user = Repo::user()->get($userId, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach the validation checks for this form
|
||||
*
|
||||
* @param PKPRequest|null $request
|
||||
*/
|
||||
public function attachValidationChecks($request = null): self
|
||||
{
|
||||
$request ??= Application::get()->getRequest();
|
||||
$site = $request->getSite();
|
||||
$form = $this;
|
||||
|
||||
if (!$this->user) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'username', 'required', 'user.profile.form.usernameRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'username', 'required', 'user.register.form.usernameExists', function ($username, $userId) {
|
||||
$user = Repo::user()->getByUsername($username, true);
|
||||
return !$user || $user->getId() == $userId;
|
||||
}, [$this->userId]));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorUsername($this, 'username', 'required', 'user.register.form.usernameAlphaNumeric'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'password', 'required', 'user.profile.form.passwordRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordLengthRestriction', function ($password) use ($form, $site) {
|
||||
return $form->getData('generatePassword') || PKPString::strlen($password) >= $site->getMinPasswordLength();
|
||||
}, [], false, ['length' => $site->getMinPasswordLength()]));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'password', 'required', 'user.register.form.passwordsDoNotMatch', function ($password) use ($form) {
|
||||
return $password == $form->getData('password2');
|
||||
}));
|
||||
} else {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'password', 'optional', 'user.register.form.passwordLengthRestriction', function ($password) use ($form, $site) {
|
||||
return $form->getData('generatePassword') || PKPString::strlen($password) >= $site->getMinPasswordLength();
|
||||
}, [], false, ['length' => $site->getMinPasswordLength()]));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'password', 'optional', 'user.register.form.passwordsDoNotMatch', function ($password) use ($form) {
|
||||
return $password == $form->getData('password2');
|
||||
}));
|
||||
}
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorLocale($this, 'givenName', 'required', 'user.profile.form.givenNameRequired', $site->getPrimaryLocale()));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'familyName', 'optional', 'user.profile.form.givenNameRequired.locale', function ($familyName) use ($form) {
|
||||
$givenNames = $form->getData('givenName');
|
||||
foreach ($familyName as $locale => $value) {
|
||||
if (!empty($value) && empty($givenNames[$locale])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorUrl($this, 'userUrl', 'optional', 'user.profile.form.urlInvalid'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorEmail($this, 'email', 'required', 'user.profile.form.emailRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCustom($this, 'email', 'required', 'user.register.form.emailExists', function ($email, $currentUserId) {
|
||||
$user = Repo::user()->getByEmail($email, true);
|
||||
return !$user || $user->getId() == $currentUserId;
|
||||
}, [$this->userId]));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorORCID($this, 'orcid', 'optional', 'user.orcid.orcidInvalid'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the update only for user's user group
|
||||
*
|
||||
*/
|
||||
public function applyUserGroupUpdateOnly(): self
|
||||
{
|
||||
$this->userGroupUpdateOnly = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current user profile.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
$request = Application::get()->getRequest();
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
|
||||
$templateMgr->assign('site', $request->getSite());
|
||||
|
||||
$data = [];
|
||||
|
||||
if (isset($this->user)) {
|
||||
$user = $this->user;
|
||||
$templateMgr->assign('user', $user);
|
||||
$interestManager = new InterestManager();
|
||||
|
||||
$data = [
|
||||
'username' => $user->getUsername(),
|
||||
'givenName' => $user->getGivenName(null), // Localized
|
||||
'familyName' => $user->getFamilyName(null), // Localized
|
||||
'preferredPublicName' => $user->getPreferredPublicName(null), // Localized
|
||||
'signature' => $user->getSignature(null), // Localized
|
||||
'affiliation' => $user->getAffiliation(null), // Localized
|
||||
'email' => $user->getEmail(),
|
||||
'userUrl' => $user->getUrl(),
|
||||
'phone' => $user->getPhone(),
|
||||
'orcid' => $user->getOrcid(),
|
||||
'mailingAddress' => $user->getMailingAddress(),
|
||||
'country' => $user->getCountry(),
|
||||
'biography' => $user->getBiography(null), // Localized
|
||||
'interests' => $interestManager->getInterestsForUser($user),
|
||||
'locales' => $user->getLocales(),
|
||||
];
|
||||
$data['canCurrentUserGossip'] = Repo::user()->canCurrentUserGossip($user->getId());
|
||||
if ($data['canCurrentUserGossip']) {
|
||||
$data['gossip'] = $user->getGossip();
|
||||
}
|
||||
} elseif (isset($this->author)) {
|
||||
$author = $this->author;
|
||||
$templateMgr->assign('user', $author);
|
||||
$data = [
|
||||
'givenName' => $author->getGivenName(null), // Localized
|
||||
'familyName' => $author->getFamilyName(null), // Localized
|
||||
'affiliation' => $author->getAffiliation(null), // Localized
|
||||
'preferredPublicName' => $author->getPreferredPublicName(null), // Localized
|
||||
'email' => $author->getEmail(),
|
||||
'userUrl' => $author->getUrl(),
|
||||
'orcid' => $author->getOrcid(),
|
||||
'country' => $author->getCountry(),
|
||||
'biography' => $author->getBiography(null), // Localized
|
||||
];
|
||||
} else {
|
||||
$data = [
|
||||
'mustChangePassword' => true,
|
||||
];
|
||||
}
|
||||
foreach ($data as $key => $value) {
|
||||
$this->setData($key, $value);
|
||||
}
|
||||
|
||||
parent::initData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc UserForm::display
|
||||
*
|
||||
* @param null|mixed $request
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function display($request = null, $template = null)
|
||||
{
|
||||
$site = $request->getSite();
|
||||
$countries = [];
|
||||
foreach (Locale::getCountries() as $country) {
|
||||
$countries[$country->getAlpha2()] = $country->getLocalName();
|
||||
}
|
||||
asort($countries);
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
|
||||
$templateMgr->assign([
|
||||
'minPasswordLength' => $site->getMinPasswordLength(),
|
||||
'source' => $request->getUserVar('source'),
|
||||
'userId' => $this->userId,
|
||||
'sitePrimaryLocale' => $site->getPrimaryLocale(),
|
||||
'availableLocales' => $site->getSupportedLocaleNames(),
|
||||
'countries' => $countries,
|
||||
'userGroupUpdateOnly' => $this->userGroupUpdateOnly,
|
||||
]);
|
||||
|
||||
if (isset($this->user)) {
|
||||
$templateMgr->assign('username', $this->user->getUsername());
|
||||
}
|
||||
|
||||
return parent::display($request, $template);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*
|
||||
* @see Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
parent::readInputData();
|
||||
|
||||
// if doing only a partial update that includes only updating user's user group
|
||||
if ($this->userGroupUpdateOnly) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->readUserVars([
|
||||
'password',
|
||||
'password2',
|
||||
'givenName',
|
||||
'familyName',
|
||||
'preferredPublicName',
|
||||
'signature',
|
||||
'affiliation',
|
||||
'email',
|
||||
'userUrl',
|
||||
'phone',
|
||||
'orcid',
|
||||
'mailingAddress',
|
||||
'country',
|
||||
'biography',
|
||||
'gossip',
|
||||
'interests',
|
||||
'locales',
|
||||
'generatePassword',
|
||||
'sendNotify',
|
||||
'mustChangePassword'
|
||||
]);
|
||||
if ($this->userId == null) {
|
||||
$this->readUserVars(['username']);
|
||||
}
|
||||
|
||||
if ($this->getData('locales') == null || !is_array($this->getData('locales'))) {
|
||||
$this->setData('locales', []);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all locale field names
|
||||
*/
|
||||
public function getLocaleFieldNames()
|
||||
{
|
||||
return ['biography', 'signature', 'affiliation', Identity::IDENTITY_SETTING_GIVENNAME, Identity::IDENTITY_SETTING_FAMILYNAME, 'preferredPublicName'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update a user.
|
||||
*/
|
||||
public function execute(...$functionParams)
|
||||
{
|
||||
$request = Application::get()->getRequest();
|
||||
$context = $request->getContext();
|
||||
|
||||
if (!isset($this->user)) {
|
||||
$this->user = Repo::user()->newDataObject();
|
||||
$this->user->setInlineHelp(1); // default new users to having inline help visible
|
||||
}
|
||||
|
||||
//save the user's user group assignment
|
||||
$this->saveUserGroupAssignments($request);
|
||||
|
||||
// if doing only a partial update that includes only updating user's user group
|
||||
if ($this->userGroupUpdateOnly) {
|
||||
parent::execute(...$functionParams);
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
$this->user->setGivenName($this->getData('givenName'), null); // Localized
|
||||
$this->user->setFamilyName($this->getData('familyName'), null); // Localized
|
||||
$this->user->setPreferredPublicName($this->getData('preferredPublicName'), null); // Localized
|
||||
$this->user->setAffiliation($this->getData('affiliation'), null); // Localized
|
||||
$this->user->setSignature($this->getData('signature'), null); // Localized
|
||||
$this->user->setEmail($this->getData('email'));
|
||||
$this->user->setUrl($this->getData('userUrl'));
|
||||
$this->user->setPhone($this->getData('phone'));
|
||||
$this->user->setOrcid($this->getData('orcid'));
|
||||
$this->user->setMailingAddress($this->getData('mailingAddress'));
|
||||
$this->user->setCountry($this->getData('country'));
|
||||
$this->user->setBiography($this->getData('biography'), null); // Localized
|
||||
$this->user->setMustChangePassword($this->getData('mustChangePassword') ? 1 : 0);
|
||||
|
||||
// Users can never view/edit their own gossip fields
|
||||
if (Repo::user()->canCurrentUserGossip($this->user->getId())) {
|
||||
$this->user->setGossip($this->getData('gossip'));
|
||||
}
|
||||
|
||||
$site = $request->getSite();
|
||||
$availableLocales = $site->getSupportedLocales();
|
||||
|
||||
$locales = [];
|
||||
foreach ($this->getData('locales') as $locale) {
|
||||
if (Locale::isLocaleValid($locale) && in_array($locale, $availableLocales)) {
|
||||
array_push($locales, $locale);
|
||||
}
|
||||
}
|
||||
$this->user->setLocales($locales);
|
||||
|
||||
parent::execute(...$functionParams);
|
||||
|
||||
if ($this->user->getId() != null) {
|
||||
if ($this->getData('password') !== '') {
|
||||
$this->user->setPassword(Validation::encryptCredentials($this->user->getUsername(), $this->getData('password')));
|
||||
|
||||
$sessionManager = SessionManager::getManager();
|
||||
$sessionManager->invalidateSessions(
|
||||
$this->user->getId(),
|
||||
(int) $this->user->getId() === (int) $request->getUser()->getId()
|
||||
? $sessionManager->getUserSession()->getId()
|
||||
: null
|
||||
);
|
||||
}
|
||||
|
||||
Repo::user()->edit($this->user);
|
||||
} else {
|
||||
$this->user->setUsername($this->getData('username'));
|
||||
if ($this->getData('generatePassword')) {
|
||||
$password = Validation::generatePassword();
|
||||
$sendNotify = true;
|
||||
} else {
|
||||
$password = $this->getData('password');
|
||||
$sendNotify = $this->getData('sendNotify');
|
||||
}
|
||||
|
||||
$this->user->setPassword(Validation::encryptCredentials($this->getData('username'), $password));
|
||||
|
||||
$this->user->setDateRegistered(Core::getCurrentDate());
|
||||
Repo::user()->add($this->user);
|
||||
|
||||
if ($sendNotify) {
|
||||
// Send welcome email to user
|
||||
$mailable = new UserCreated($context, $password);
|
||||
$mailable->recipients($this->user);
|
||||
$mailable->sender($request->getUser());
|
||||
$mailable->replyTo($context->getData('contactEmail'), $context->getData('contactName'));
|
||||
$template = Repo::emailTemplate()->getByKey($context->getId(), UserCreated::getEmailTemplateKey());
|
||||
$mailable->body($template->getLocalizedData('body'));
|
||||
$mailable->subject($template->getLocalizedData('subject'));
|
||||
|
||||
try {
|
||||
Mail::send($mailable);
|
||||
} catch (TransportException $e) {
|
||||
$notificationMgr = new NotificationManager();
|
||||
$notificationMgr->createTrivialNotification(
|
||||
$request->getUser()->getId(),
|
||||
PKPNotification::NOTIFICATION_TYPE_ERROR,
|
||||
['contents' => __('email.compose.error')]
|
||||
);
|
||||
error_log($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$interestManager = new InterestManager();
|
||||
$interestManager->setInterestsForUser($this->user, $this->getData('interests'));
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/user/form/UserDisableForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserDisableForm
|
||||
*
|
||||
* @ingroup controllers_grid_settings_user_form
|
||||
*
|
||||
* @brief Form for enabling/disabling a user
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\user\form;
|
||||
|
||||
use APP\facades\Repo;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\form\Form;
|
||||
use PKP\db\DAORegistry;
|
||||
|
||||
class UserDisableForm extends Form
|
||||
{
|
||||
/** @var int The user id of user to enable/disable */
|
||||
public $_userId;
|
||||
|
||||
/** @var bool Whether to enable or disable the user */
|
||||
public $_enable;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct($userId, $enable = false)
|
||||
{
|
||||
parent::__construct('controllers/grid/settings/user/form/userDisableForm.tpl');
|
||||
|
||||
$this->_userId = (int) $userId;
|
||||
$this->_enable = (bool) $enable;
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
if ($this->_userId) {
|
||||
$user = Repo::user()->get($this->_userId, true);
|
||||
|
||||
if ($user) {
|
||||
$this->_data = [
|
||||
'disableReason' => $user->getDisabledReason()
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*
|
||||
* @see Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(
|
||||
[
|
||||
'disableReason',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::display
|
||||
*
|
||||
* @param null|mixed $request
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function display($request = null, $template = null)
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
$templateMgr->assign([
|
||||
'userId' => $this->_userId,
|
||||
'enable' => $this->_enable,
|
||||
]);
|
||||
return $this->fetch($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$user = Repo::user()->get($this->_userId, true);
|
||||
|
||||
if ($user) {
|
||||
$user->setDisabled($this->_enable ? false : true);
|
||||
$user->setDisabledReason($this->getData('disableReason'));
|
||||
Repo::user()->edit($user);
|
||||
if ($user->getDisabled()) {
|
||||
$sessionDao = DAORegistry::getDAO('SessionDAO');
|
||||
$sessionDao->deleteByUserId($user->getId());
|
||||
}
|
||||
}
|
||||
parent::execute(...$functionArgs);
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/user/form/UserEmailForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserEmailForm
|
||||
*
|
||||
* @ingroup controllers_grid_settings_user_form
|
||||
*
|
||||
* @brief Form for sending an email to a user
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\user\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use APP\notification\NotificationManager;
|
||||
use APP\template\TemplateManager;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use PKP\form\Form;
|
||||
use PKP\mail\Mailable;
|
||||
use PKP\notification\PKPNotification;
|
||||
|
||||
class UserEmailForm extends Form
|
||||
{
|
||||
/** @var int The user id of user to send email to */
|
||||
public $userId;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $userId User ID to contact.
|
||||
*/
|
||||
public function __construct($userId)
|
||||
{
|
||||
parent::__construct('controllers/grid/settings/user/form/userEmailForm.tpl');
|
||||
|
||||
$this->userId = (int) $userId;
|
||||
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'subject', 'required', 'email.subjectRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'message', 'required', 'email.bodyRequired'));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign form data to user-submitted data.
|
||||
*
|
||||
* @see Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars([
|
||||
'subject',
|
||||
'message',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::Fetch
|
||||
*
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function fetch($request, $template = null, $display = false)
|
||||
{
|
||||
$user = Repo::user()->get($this->userId);
|
||||
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
$templateMgr->assign([
|
||||
'userId' => $this->userId,
|
||||
'userFullName' => $user->getFullName(),
|
||||
'userEmail' => $user->getEmail(),
|
||||
]);
|
||||
|
||||
return parent::fetch($request, $template, $display);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the email
|
||||
*
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
$toUser = Repo::user()->get($this->userId);
|
||||
$request = Application::get()->getRequest();
|
||||
$fromUser = $request->getUser();
|
||||
|
||||
$mailable = new Mailable();
|
||||
$mailable
|
||||
->from($fromUser->getEmail(), $fromUser->getFullName())
|
||||
->to($toUser->getEmail(), $toUser->getFullName())
|
||||
->subject($this->getData('subject'))
|
||||
->body($this->getData('message'));
|
||||
|
||||
parent::execute(...$functionArgs);
|
||||
|
||||
try {
|
||||
Mail::send($mailable);
|
||||
} catch (Exception $e) {
|
||||
$notificationMgr = new NotificationManager();
|
||||
$notificationMgr->createTrivialNotification(
|
||||
$request->getUser()->getId(),
|
||||
PKPNotification::NOTIFICATION_TYPE_ERROR,
|
||||
['contents' => __('email.compose.error')]
|
||||
);
|
||||
error_log($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/user/form/UserForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserForm
|
||||
*
|
||||
* @ingroup controllers_grid_settings_user_form
|
||||
*
|
||||
* @brief Base class for user forms.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\user\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\core\Request;
|
||||
use APP\facades\Repo;
|
||||
use APP\template\TemplateManager;
|
||||
use PKP\form\Form;
|
||||
|
||||
class UserForm extends Form
|
||||
{
|
||||
/** @var int Id of the user being edited */
|
||||
public $userId;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $userId optional
|
||||
*/
|
||||
public function __construct($template, $userId = null)
|
||||
{
|
||||
parent::__construct($template);
|
||||
|
||||
$this->userId = isset($userId) ? (int) $userId : null;
|
||||
|
||||
if (!is_null($userId)) {
|
||||
$this->addCheck(new \PKP\form\validation\FormValidator($this, 'userGroupIds', 'required', 'manager.users.roleRequired'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize form data from current user profile.
|
||||
*/
|
||||
public function initData()
|
||||
{
|
||||
$userGroupIds = [];
|
||||
|
||||
if (!is_null($this->userId)) {
|
||||
$userGroups = Repo::userGroup()->userUserGroups($this->userId);
|
||||
|
||||
foreach ($userGroups as $userGroup) {
|
||||
$userGroupIds[] = $userGroup->getId();
|
||||
}
|
||||
}
|
||||
|
||||
$this->setData('userGroupIds', $userGroupIds);
|
||||
|
||||
|
||||
parent::initData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::readInputData()
|
||||
*/
|
||||
public function readInputData()
|
||||
{
|
||||
$this->readUserVars(['userGroupIds']);
|
||||
parent::readInputData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::display
|
||||
*
|
||||
* @param null|mixed $request
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function display($request = null, $template = null)
|
||||
{
|
||||
$context = $request->getContext();
|
||||
$contextId = $context ? $context->getId() : \PKP\core\PKPApplication::CONTEXT_ID_NONE;
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
|
||||
$allUserGroups = [];
|
||||
|
||||
$userGroups = Repo::userGroup()->getCollector()
|
||||
->filterByContextIds([$contextId])
|
||||
->getMany();
|
||||
|
||||
foreach ($userGroups as $userGroup) {
|
||||
$allUserGroups[(int) $userGroup->getId()] = $userGroup->getLocalizedName();
|
||||
}
|
||||
|
||||
$templateMgr->assign([
|
||||
'allUserGroups' => $allUserGroups,
|
||||
'assignedUserGroups' => array_map('intval', $this->getData('userGroupIds')),
|
||||
]);
|
||||
|
||||
return $this->fetch($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Form::execute()
|
||||
*/
|
||||
public function execute(...$functionArgs)
|
||||
{
|
||||
parent::execute(...$functionArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the user group assignments
|
||||
*/
|
||||
public function saveUserGroupAssignments(Request $request): void
|
||||
{
|
||||
if (!isset($this->userId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Repo::userGroup()
|
||||
->deleteAssignmentsByContextId(
|
||||
Application::get()->getRequest()->getContext()->getId(),
|
||||
$this->userId
|
||||
);
|
||||
|
||||
|
||||
if ($this->getData('userGroupIds')) {
|
||||
$contextId = $request->getContext()->getId();
|
||||
|
||||
collect($this->getData('userGroupIds'))
|
||||
->each(
|
||||
fn ($userGroupId) =>
|
||||
Repo::userGroup()->contextHasGroup($contextId, $userGroupId)
|
||||
? Repo::userGroup()->assignUserToGroup($this->userId, $userGroupId)
|
||||
: null
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file controllers/grid/settings/user/form/UserRoleForm.php
|
||||
*
|
||||
* Copyright (c) 2014-2021 Simon Fraser University
|
||||
* Copyright (c) 2003-2021 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class UserRoleForm
|
||||
*
|
||||
* @ingroup controllers_grid_settings_user_form
|
||||
*
|
||||
* @brief Form for managing roles for a newly created user.
|
||||
*/
|
||||
|
||||
namespace PKP\controllers\grid\settings\user\form;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use APP\template\TemplateManager;
|
||||
|
||||
class UserRoleForm extends UserForm
|
||||
{
|
||||
/** @var string User full name */
|
||||
public $_userFullName;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $userId
|
||||
* @param string $userFullName
|
||||
*/
|
||||
public function __construct($userId, $userFullName)
|
||||
{
|
||||
parent::__construct('controllers/grid/settings/user/form/userRoleForm.tpl', $userId);
|
||||
|
||||
$this->_userFullName = $userFullName;
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorPost($this));
|
||||
$this->addCheck(new \PKP\form\validation\FormValidatorCSRF($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc UserForm::display
|
||||
*
|
||||
* @param null|mixed $request
|
||||
* @param null|mixed $template
|
||||
*/
|
||||
public function display($request = null, $template = null)
|
||||
{
|
||||
$templateMgr = TemplateManager::getManager($request);
|
||||
$templateMgr->assign([
|
||||
'userId' => $this->userId,
|
||||
'userFullName' => $this->_userFullName,
|
||||
]);
|
||||
return parent::display($request, $template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user's roles.
|
||||
*/
|
||||
public function execute(...$functionParams)
|
||||
{
|
||||
$request = Application::get()->getRequest();
|
||||
|
||||
//save the user's user group assignment
|
||||
$this->saveUserGroupAssignments($request);
|
||||
|
||||
parent::execute(...$functionParams);
|
||||
|
||||
// Role management handled by parent form, just return user.
|
||||
return Repo::user()->get($this->userId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user