120 lines
4.0 KiB
PHP
120 lines
4.0 KiB
PHP
<?php
|
|
namespace OCA\Agency\Agency;
|
|
|
|
use OCP\IRequest;
|
|
use OCP\IUserSession;
|
|
use OCP\IUserManager;
|
|
use OCP\IDBConnection;
|
|
use OCA\Agency\Db\AgencyMapper;
|
|
use OCA\Agency\Service\AgencyService;
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
class AgencyManager {
|
|
|
|
protected $agencyMapper;
|
|
protected $groupManager;
|
|
protected $userSession;
|
|
protected $userManager;
|
|
protected $db;
|
|
|
|
public function __construct($AppName, IDBConnection $db, IRequest $request, AgencyMapper $agencyMapper, IGroupManager $groupManager, IUserSession $userSession, IUserManager $userManager) {
|
|
$this->agencyMapper = $agencyMapper;
|
|
$this->groupManager = $groupManager;
|
|
$this->userSession = $userSession;
|
|
$this->userManager = $userManager;
|
|
}
|
|
/**
|
|
* Creates a new Agency and save it to the database.
|
|
*
|
|
* @param:
|
|
* agencygid - GrouopID of the Agency
|
|
* agencydirid - ID for the Grouopfolder for AgencyData
|
|
* standarddirid - ID for the Groupfolder for all Uploaded Standardfiles
|
|
*/
|
|
public function createAgencyOnReg(string $agencygid, int $agencydirid, int $standarddirid){
|
|
$agencyService = new AgencyService($this->agencyMapper);
|
|
return $agencyService->create($agencygid, $agencydirid, $standarddirid);
|
|
}
|
|
|
|
/**
|
|
* getAgencyByUser
|
|
*
|
|
* Return the complete Agency-Data by filtering the id from default agency-group, ex: agency_1 -> 1 (int)
|
|
*
|
|
*/
|
|
public function getAgencyByUser(){
|
|
$groups = $this->groupManager->getUserGroups($this->userSession->getUser());
|
|
$agency_group_id = "";
|
|
foreach($groups as $group){
|
|
if(str_contains($group->getGId(), 'agencymaingroupid_')){
|
|
$agency_group_id_array = explode("_", strval($group->getGId()));
|
|
$agency_group_id = $agency_group_id_array[0]."_".$agency_group_id_array[1];
|
|
}
|
|
}
|
|
return $this->agencyMapper->getAgencyByGId($agency_group_id);
|
|
#return $agency_group_id;
|
|
}
|
|
/**
|
|
* Return only the AgencyGroupId
|
|
*/
|
|
public function getAgencyGIdByUser(){
|
|
$groups = $this->groupManager->getUserGroups($this->userSession->getUser());
|
|
$agency_group_id = "";
|
|
foreach($groups as $group){
|
|
if(str_contains($group->getGId(), 'agencymaingroupid_')){
|
|
$agency_group_id_array = explode("_", strval($group->getGId()));
|
|
$agency_group_id = $agency_group_id_array[0]."_".$agency_group_id_array[1];
|
|
}
|
|
}
|
|
return $agency_group_id;
|
|
}
|
|
|
|
// Return an random Id-Ele for generating single IDs
|
|
public function generateRandomId($length = 6) {
|
|
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
$charactersLength = strlen($characters);
|
|
$randomString = '';
|
|
for ($i = 0; $i < $length; $i++) {
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
}
|
|
return $randomString;
|
|
}
|
|
|
|
/**
|
|
* Returns all users of an Agency by filtering the Main Agency Group Id out
|
|
*
|
|
* Returns an Array of IUser-Objects. Filtering is by logged User
|
|
*/
|
|
public function getagencycontributors(){
|
|
$group = $this->groupManager->get($this->getAgencyGIdByUser());
|
|
$users = $group->getUsers();
|
|
|
|
$return_users = array();
|
|
// Creating a nice User-JSON-Array
|
|
foreach($users as $user){
|
|
array_push($return_users, array("user" => $user->getUID(), "displayName" => $user->getDisplayName()));
|
|
}
|
|
return $return_users;
|
|
}
|
|
|
|
# TODO: Hier prüfen, ob alle User in der gleichen Agentur sind und er eingeloggte User auch wirklich das Recht dazu hat
|
|
public function updateagencygroupcontributors(string $gid, array $users){
|
|
$groupToChange = $this->groupManager->get($gid);
|
|
$usersInGroup = $groupToChange->getUsers();
|
|
|
|
foreach($usersInGroup as $user){
|
|
if($groupToChange->canRemoveUser()){
|
|
$groupToChange->removeUser($user);
|
|
}
|
|
}
|
|
|
|
foreach($users as $user) {
|
|
if($groupToChange->canAddUser()){
|
|
$groupToChange->addUser($this->userManager->get($user));
|
|
}
|
|
}
|
|
return $users;
|
|
}
|
|
|
|
} |