Some checks are pending
Module sub-split / Sub-split (push) Waiting to run
- Base: wintercms/winter branch 1.2 (full framework) - Theme vivespos: Canvas 7 + Bootstrap 5 CDN, custom CSS - Layout: deferred GTM/GA4 tracking, JSON-LD SoftwareApplication - Partials: hero (offline-first), features, modes (offline/nube toggle), screenshots, pricing (3 planes), comparison, FAQ, CTA - Plugin VivesPOS.Site with ContactForm - Dockerfile: PHP 8.2 Apache, port 80, healthcheck - Added winter/wn-pages, blog, sitemap, seo plugins - Active theme set to vivespos
259 lines
6.7 KiB
PHP
259 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace Backend\Controllers;
|
|
|
|
use Backend\Behaviors\FormController;
|
|
use Backend\Behaviors\ListController;
|
|
use Backend\Behaviors\RelationController;
|
|
use Backend\Classes\Controller;
|
|
use Backend\Facades\Backend;
|
|
use Backend\Facades\BackendAuth;
|
|
use Backend\Facades\BackendMenu;
|
|
use Backend\Models\UserGroup;
|
|
use Illuminate\Support\Facades\Lang;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\Response;
|
|
use Illuminate\Support\Str;
|
|
use System\Classes\SettingsManager;
|
|
use Winter\Storm\Support\Facades\Flash;
|
|
use Winter\Storm\Support\Facades\Mail;
|
|
|
|
/**
|
|
* Backend user controller
|
|
*
|
|
* @package winter\wn-backend-module
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
*
|
|
*/
|
|
class Users extends Controller
|
|
{
|
|
/**
|
|
* @var array Extensions implemented by this controller.
|
|
*/
|
|
public $implement = [
|
|
FormController::class,
|
|
ListController::class,
|
|
RelationController::class,
|
|
];
|
|
|
|
/**
|
|
* @var array Permissions required to view this page.
|
|
*/
|
|
public $requiredPermissions = ['backend.manage_users'];
|
|
|
|
/**
|
|
* @var string HTML body tag class
|
|
*/
|
|
public $bodyClass = 'compact-container';
|
|
|
|
public $formLayout = 'sidebar';
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
BackendMenu::setContext('Winter.System', 'system', 'users');
|
|
SettingsManager::setContext('Winter.System', 'administrators');
|
|
}
|
|
|
|
/**
|
|
* Extends the list query to hide superusers if the current user is not a superuser themselves
|
|
*/
|
|
public function listExtendQuery($query)
|
|
{
|
|
if (!$this->user->isSuperUser()) {
|
|
$query->where('is_superuser', false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prevents non-superusers from even seeing the is_superuser filter
|
|
*/
|
|
public function listFilterExtendScopes($filterWidget)
|
|
{
|
|
if (!$this->user->isSuperUser()) {
|
|
$filterWidget->removeScope('is_superuser');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Strike out deleted records
|
|
*/
|
|
public function listInjectRowClass($record, $definition = null)
|
|
{
|
|
if ($record->trashed()) {
|
|
return 'strike';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Extends the form query to prevent non-superusers from accessing superusers at all
|
|
*/
|
|
public function formExtendQuery($query)
|
|
{
|
|
if (!$this->user->isSuperUser()) {
|
|
$query->where('is_superuser', false);
|
|
}
|
|
|
|
// Ensure soft-deleted records can still be managed
|
|
$query->withTrashed();
|
|
}
|
|
|
|
/**
|
|
* Before creating a new user, generate password if auto-generate is enabled
|
|
*/
|
|
public function formBeforeCreate($model)
|
|
{
|
|
if (post('User._auto_generate_password')) {
|
|
$password = Str::random(22);
|
|
$model->password = $password;
|
|
$model->password_confirmation = $password;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update controller
|
|
*/
|
|
public function update($recordId, $context = null)
|
|
{
|
|
// Users cannot edit themselves, only use My Account
|
|
if ($context != 'myaccount' && $recordId == $this->user->id) {
|
|
return Backend::redirect('backend/myaccount');
|
|
}
|
|
|
|
return $this->asExtension('FormController')->update($recordId, $context);
|
|
}
|
|
|
|
/**
|
|
* Handle restoring users
|
|
*/
|
|
public function update_onRestore($recordId)
|
|
{
|
|
$this->formFindModelObject($recordId)->restore();
|
|
|
|
Flash::success(Lang::get('backend::lang.form.restore_success', ['name' => Lang::get('backend::lang.user.name')]));
|
|
|
|
return Redirect::refresh();
|
|
}
|
|
|
|
/**
|
|
* Impersonate this user
|
|
*/
|
|
public function update_onImpersonateUser($recordId)
|
|
{
|
|
if (!$this->user->hasAccess('backend.impersonate_users')) {
|
|
return Response::make(Lang::get('backend::lang.page.access_denied.label'), 403);
|
|
}
|
|
|
|
$model = $this->formFindModelObject($recordId);
|
|
|
|
BackendAuth::impersonate($model);
|
|
|
|
Flash::success(Lang::get('backend::lang.account.impersonate_success'));
|
|
|
|
return Backend::redirect('backend/myaccount');
|
|
}
|
|
|
|
/**
|
|
* Unsuspend this user
|
|
*/
|
|
public function update_onUnsuspendUser($recordId)
|
|
{
|
|
$model = $this->formFindModelObject($recordId);
|
|
|
|
$model->unsuspend();
|
|
|
|
Flash::success(Lang::get('backend::lang.account.unsuspend_success'));
|
|
|
|
return Redirect::refresh();
|
|
}
|
|
|
|
/**
|
|
* Backward compatibility redirect to the new MyAccount controller.
|
|
*/
|
|
public function myaccount()
|
|
{
|
|
return Backend::redirect('backend/myaccount');
|
|
}
|
|
|
|
/**
|
|
* Add available permission fields to the User form.
|
|
* Mark default groups as checked for new Users.
|
|
*/
|
|
public function formExtendFields($form)
|
|
{
|
|
if ($form->getContext() == 'myaccount') {
|
|
return;
|
|
}
|
|
|
|
if (!$this->user->isSuperUser()) {
|
|
$form->removeField('is_superuser');
|
|
}
|
|
|
|
/*
|
|
* Add permissions tab
|
|
*/
|
|
$form->addTabFields($this->generatePermissionsField());
|
|
|
|
/*
|
|
* Mark default groups
|
|
*/
|
|
if (!$form->model->exists) {
|
|
$defaultGroupIds = UserGroup::where('is_new_user_default', true)->lists('id');
|
|
|
|
$groupField = $form->getField('groups');
|
|
if ($groupField) {
|
|
$groupField->value = $defaultGroupIds;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds the permissions editor widget to the form.
|
|
* @return array
|
|
*/
|
|
protected function generatePermissionsField()
|
|
{
|
|
return [
|
|
'permissions' => [
|
|
'tab' => 'backend::lang.user.permissions',
|
|
'type' => 'Backend\FormWidgets\PermissionEditor',
|
|
'trigger' => [
|
|
'action' => 'disable',
|
|
'field' => 'is_superuser',
|
|
'condition' => 'checked'
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Send password reset mail
|
|
*/
|
|
public function update_onManualPasswordReset($recordId)
|
|
{
|
|
$user = $this->formFindModelObject($recordId);
|
|
|
|
if ($user) {
|
|
$code = $user->getResetPasswordCode();
|
|
$link = Backend::url('backend/auth/reset/' . $user->id . '/' . $code);
|
|
|
|
$data = [
|
|
'name' => $user->full_name,
|
|
'link' => $link,
|
|
];
|
|
|
|
Mail::send('backend::mail.restore', $data, function ($message) use ($user) {
|
|
$message->to($user->email, $user->full_name)->subject(trans('backend::lang.account.password_reset'));
|
|
});
|
|
}
|
|
|
|
Flash::success(Lang::get('backend::lang.account.manual_password_reset_success'));
|
|
|
|
return Redirect::refresh();
|
|
}
|
|
}
|