浏览代码

[Backend] Access : refactoring

feature/souke
Guillaume Bourgeois 1年前
父节点
当前提交
5d8e45666a
共有 3 个文件被更改,包括 40 次插入14 次删除
  1. +16
    -11
      backend/controllers/AccessController.php
  2. +6
    -0
      common/logic/User/User/Repository/UserRepository.php
  3. +18
    -3
      common/logic/User/User/Service/UserBuilder.php

+ 16
- 11
backend/controllers/AccessController.php 查看文件

@@ -75,25 +75,28 @@ class AccessController extends BackendController
/**
* Affiche les utilisateurs ayant accès à l'administration de ce producteur.
* Gestion du formulaire permettant d'en ajouter de nouveaux.
*
* @return string
*/
public function actionIndex()
{
$userManager = $this->getUserManager();
$producer = $this->getProducerCurrent();
$userSearch = new UserSearch();
$usersArray = $userSearch->search()->query->all();

$modelAccessUserProducerForm = new AccessUserProducerForm;
if ($modelAccessUserProducerForm->load(\Yii::$app->request->post()) && $modelAccessUserProducerForm->save()) {
$this->setFlash('success', 'Droits ajoutés à l\'utilisateur');
if ($modelAccessUserProducerForm->load(\Yii::$app->request->post()) && $modelAccessUserProducerForm->validate()) {
$user = $userManager->findOneUserById($modelAccessUserProducerForm->id_user);
if($user && $userManager->grantAccess($user)) {
$this->setFlash('success', 'Droits ajoutés à l\'utilisateur');
}
else {
$this->addFlash('error', "Une erreur est survenue.");
}
}

$producer = $this->getProducerCurrent();
$usersAccessArray = $this->getUserManager()->findUsersByProducer($producer);

return $this->render('index', [
'usersArray' => $usersArray,
'usersAccessArray' => $usersAccessArray,
'usersAccessArray' => $userManager->findUsersByProducer($producer),
'producer' => $producer,
'modelAccessUserProducerForm' => $modelAccessUserProducerForm,
]);
@@ -104,9 +107,11 @@ class AccessController extends BackendController
$userManager = $this->getUserManager();
$user = $userManager->findOneUserById($idUser);

if ($user) {
$userManager->deleteAccess($user);
$this->setFlash('success', 'Droits de l\'utilisateur supprimé.');
if ($user && $userManager->deleteAccess($user)) {
$this->addFlash('success', 'Droits de l\'utilisateur supprimé.');
}
else {
$this->addFlash('error', "Une erreur est survenue.");
}

return $this->redirect(['index']);

+ 6
- 0
common/logic/User/User/Repository/UserRepository.php 查看文件

@@ -224,4 +224,10 @@ class UserRepository extends AbstractRepository

return count($results);
}

public function isActive(User $user)
{
$userProducer = $this->userProducerRepository->findOneUserProducer($user);
return $userProducer ? $userProducer->active : false;
}
}

+ 18
- 3
common/logic/User/User/Service/UserBuilder.php 查看文件

@@ -76,10 +76,25 @@ class UserBuilder extends AbstractBuilder
$user->password_reset_token = null;
}

public function grantAccess(User $user): bool
{
if($this->userRepository->isActive($user)) {
$user->id_producer = $this->getProducerContextId();
$user->status = User::STATUS_PRODUCER;
return $user->save();
}

return false;
}

public function deleteAccess(User $user): bool
{
$user->id_producer = 0;
$user->status = User::STATUS_ACTIVE;
return $user->save();
if($this->userRepository->isActive($user)) {
$user->id_producer = null;
$user->status = User::STATUS_ACTIVE;
return $user->save();
}

return false;
}
}

正在加载...
取消
保存