Authentication/view/view.admin.authentication.php

144 lines
5.0 KiB
PHP

<?php
/**
* FuzeWorks Authentication Plugin.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013 - 2023 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (C) 2013 - 2023, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
namespace Application\View;
use Application\Controller\AuthenticationController;
use FuzeWorks\Administration\AdminView;
use FuzeWorks\Administration\Attributes\DisplayAttribute;
use FuzeWorks\Administration\Attributes\HiddenAttribute;
use FuzeWorks\Administration\Attributes\IconAttribute;
use FuzeWorks\Administration\Attributes\PermissionAttribute;
use FuzeWorks\Administration\Attributes\PriorityAttribute;
use FuzeWorks\Authentication\Events\GetRegisterFieldsEvent;
use FuzeWorks\Authentication\Exceptions\RegisterErrorException;
use FuzeWorks\Authentication\Exceptions\RegisterWarningException;
use FuzeWorks\Controller;
use FuzeWorks\Events;
use FuzeWorks\Forms\Fields\CheckboxField;
use FuzeWorks\Forms\Fields\EmailField;
use FuzeWorks\Forms\Fields\PasswordField;
use FuzeWorks\Forms\Fields\SubmitField;
use FuzeWorks\Forms\Form;
use FuzeWorks\Forms\Forms;
use FuzeWorks\Priority;
class AuthenticationAdminView extends AdminView
{
/** @var AuthenticationController $controller */
protected Controller $controller;
#[DisplayAttribute("Users"), IconAttribute("users"), PriorityAttribute(Priority::LOW)]
#[PermissionAttribute(["ADMIN"])]
public function users()
{
// List all users
$users = $this->controller->listUsers();
$this->layouts->assign("users", $users);
return $this->layouts->get("admin/users_list");
}
#[HiddenAttribute]
#[PermissionAttribute(["ADMIN"])]
public function view(string $userId)
{
$info = $this->controller->getDetailedUserInformation($userId);
dump($info);
exit;
return $userId;
}
#[HiddenAttribute]
#[DisplayAttribute("Create User Account")]
public function create()
{
// Prepare form
/** @var Forms $forms */
$forms = $this->libraries->get("forms");
$form = $forms->getCachedForm(function (Form $form){
// First prepare the identifier field
$field1 = new EmailField("identifier");
$field1->setLabel("Email address")
->placeholder("Email");
$form->field($field1);
// Add additional fields through GetRegisterFieldsEvent
/** @var GetRegisterFieldsEvent $event */
$event = Events::fireEvent(new GetRegisterFieldsEvent());
foreach ($event->getFields() as $field)
{
$field->setNote("eventField");
$form->field($field);
}
// Terms and conditions field
$field4 = new CheckboxField("sendemail");
$field4->setLabel("Send User Registration Notification");
$field4->checked();
$form->field($field4);
// Submit field
$field5 = new SubmitField("submit");
$field5->setButtonText("Create User Account");
$form->field($field5);
return $form;
}, "AuthCreateUserForm", "Register");
// After validation, send data to the controller
if ($form->validate())
{
// Prepare variables
$identifier = $form->getField("identifier")->getValue();
// Then forward the form to the controller
try {
$this->controller->register($identifier, null, [], [], true);
} catch (RegisterErrorException $e) {
$form->invalidate(true);
$form->addFormError($e->getMessage());
} catch (RegisterWarningException $e) {
$form->invalidate(true);
$form->addFormWarning($e->getMessage());
}
}
$this->layouts->assign("form", $form);
return $this->layouts->get("admin/users_create");
}
}