nc-vue-agency/lib/Controller/GroupController.php

131 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\Agency\Controller;
use Exception;
use OCP\IRequest;
use OCP\IGroupManager;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\Agency\Service\GroupService;
use OCA\Agency\Service\GroupPermissionService;
use OCA\Agency\Agency\AgencyManager;
class GroupController extends Controller {
private $service;
private $Id;
protected $agencymanager;
protected $userSession;
protected $groupManager;
protected $grouppermissionservice;
use Errors;
public function __construct(string $AppName, IRequest $request,
GroupService $service, GroupPermissionService $grouppermissionservice, AgencyManager $agencymanager, IGroupManager $groupManager){
parent::__construct($AppName, $request);
$this->service = $service;
$this->agencymanager = $agencymanager;
$this->groupManager = $groupManager;
$this->grouppermissionservice = $grouppermissionservice;
}
/**
* @NoAdminRequired
*
* Returns all Groups of an Agency, incl. all Data (DisplayName, GId)
*
* TODO: Checken, ob der aktuelle Nutzer das auch darf!
* TODO: Hier auch die Permissions später laden, damit das nicht nochmal requested werden muss.
*
*/
public function getagencygroups(){
$searchGroupId = $this->agencymanager->getAgencyByUser()->getAgencygid();
$groups = $this->groupManager->search($searchGroupId);
$response_groups = array();
foreach ($groups as $group) {
// Checking, if group is default or not. Default Groups cant delete and cant rename by logged user!
$isDefault = true;
if(str_contains($group->getGId(), 'subgroup')){
$isDefault = false;
}
//Checking if the Group is the Main-Group - EVERY User of this Agency should be in this group!
$isMainGroup = false;
if(sizeof(explode("_", strval($group->getGId()))) == 2){
$isMainGroup = true;
}
// Adding current Users to the Group
$user_groups = [];
foreach($group->getUsers() as $groupuser){
array_push($user_groups, array("user" => $groupuser->getUID(), "displayName" => $groupuser->getDisplayName()));
}
array_push($response_groups, array('name' => $group->getDisplayName(), 'gid' => $group->getGid(), 'isDefault' => $isDefault, 'isMainGroup' => $isMainGroup, 'users' => $user_groups));
}
//Sorting the Array A-Z by GroupDisplayName
$keys = array_column($response_groups, 'name');
array_multisort($keys, SORT_ASC, $response_groups);
return $response_groups;
}
/**
*
* Creates a new Group by GroupName
*
* Not possible are groupnames which are used to seperate the agencys
*
*
* @NoAdminRequired
*
*/
#TODO: Check, if logged user has rights to do that!
public function addagencygroup(string $groupname){
if($groupname == "agencymaingroupid" or preg_match('~[0-9]{6}+~', $groupname) == 1){
return false;
}
else {
$searchGroupId = $this->agencymanager->getAgencyByUser()->getAgencygid();
$newGroupId = $searchGroupId.'_subgroup_'.$this->agencymanager->generateRandomId();
while($this->groupManager->groupExists($newGroupId) == true){
$newGroupId = $searchGroupId.'_subgroup_'.$this->agencymanager->generateRandomId();
}
$newGroup = $this->groupManager->createGroup($newGroupId);
$newGroup->setDisplayName($groupname);
//Create the grouppermission-element
$this->grouppermissionservice->create($newGroupId);
return true;
}
}
/**
* delagencygroup
*
* Delete an AgencyGroup
*
*
* @NoAdminRequired
*/
public function delagencygroup(string $id){
# Getting the Group
$todel = $this->groupManager->get($id);
# Check, that the logged user is in the same Agency!
$searchGroupId = $this->agencymanager->getAgencyByUser()->getAgencygid();
$result = false;
# If user is in the same Agency, then delete is possible - check permissions!
# TODO: Hier noch einbauen, dass die Rechte geprüft werden!
if(str_contains($todel->getGID(), $searchGroupId)) {
$this->grouppermissionservice->remove($todel->getGID());
$todel->delete();
}
return $result;
}
}