get('session')->save(); $this->get('net15.restifier.dispatch')->map('GET',$this,'listUserGroup_GET'); $this->get('net15.restifier.dispatch')->map('POST',$this,'createGroup_POST'); $this->get('net15.restifier.dispatch')->map('PUT',$this,'updateGroup_PUT'); $this->get('net15.restifier.dispatch')->map('PATCH',$this,'joinGroup_PATCH'); return $this->get('net15.restifier.dispatch')->exec(); } public function updateGroup_PUT(){ $data = $this->get('net15.restifier.dispatch')->getUnserialized(); $actualUser = $this->getUser() ; if(!$actualUser instanceof CustomUserInterface) return $this->get('net15.restifier.dispatch')->error('Operation denied'); if(!isset($data['group']['publicKey'])) return $this->get('net15.restifier.dispatch')->error('Missing group\'s publicKey'); $group = $this->get('net15.groups.manager')->getByPublicKey($data['group']['publicKey']); if(!$group instanceof CustomUserGroupInterface) return $this->get('net15.restifier.dispatch')->error('Group not found'); if(!$this->get('net15.groups.manager')->canEdit($group,$actualUser)) return $this->get('net15.restifier.dispatch')->error('Operation denied'); if(isset($data['group']['publicVisible'] )) $data['group']['publicVisible'] = (bool)$data['group']['publicVisible']; if(isset($data['group']['publicOpen'] )) $data['group']['publicOpen'] = (bool)$data['group']['publicOpen']; $this->get('net15.restifier.process')->hydrateFromArray($group,$data['group'],['name','publicVisible','publicOpen']); $r = $this->get('net15.groups.manager')->update($group); if(!$r) return $this->get('net15.restifier.dispatch')->error('Error while registering changes'); $objectName = $this->get('net15.groups.manager')->getClassName(); if(!is_string($objectName) || empty($objectName)) return $this->get('net15.restifier.dispatch')->error('Internal error'); $retrn = $this->get('net15.restifier.process')->objectToArray($group, ['id','name','created','publicKey','publicVisible'], [ $objectName => ['id','name','created','publicKey','publicVisible'] ] ); return new JsonResponse($retrn); } /** * Use to invit in group and join, reject invitation * * @return JsonResponse */ public function joinGroup_PATCH(){ $retrn = ['accepted' => false,'rejected' => false, 'invitation' => false]; $data = $this->get('net15.restifier.dispatch')->getUnserialized(); $actualUser = $this->getUser() ; if(!$actualUser instanceof CustomUserInterface) return $this->get('net15.restifier.dispatch')->error('Operation denied'); if(!isset($data['reject'])) $data['reject'] = false; else $data['reject'] = (bool)$data['reject']; if(!isset($data['group']['publicKey'])) return $this->get('net15.restifier.dispatch')->error('Missing group\'s publicKey'); $group = $this->get('net15.groups.manager')->getByPublicKey($data['group']['publicKey']); if(!$group instanceof CustomUserGroupInterface) return $this->get('net15.restifier.dispatch')->error('Group not found'); if(!isset($data['user']['publicKey'])){ //mode accept invitation $u = $this->getUser() ; if(!$u instanceof CustomUserInterface) return $this->get('net15.restifier.dispatch')->error('Operation denied'); if(!$this->get('net15.groups.manager')->canJoin($group,$u)) return $this->get('net15.restifier.dispatch')->error('Operation denied'); if( $data['reject'] !== false){ //accepted $role = $this->get('net15.groups.manager')->setUserRoleInGroup($group,$u,CompanyRoles::USER); if(!$role instanceof CustomUserGroupRoleInterface) return $this->get('net15.restifier.dispatch')->error('Error while creating access'); $retrn['accepted'] = true; } else{ //rejected $role = $this->get('net15.groups.manager')->setUserRoleInGroup($group,$u,CompanyRoles::NOBODY); if(!$role instanceof CustomUserGroupRoleInterface) return $this->get('net15.restifier.dispatch')->error('Error while rejecting access'); $retrn['rejected'] = true; } } else { //mode invitation $u = $this->get('net15.user.manager')->getUserByPublicKey($data['user']['publicKey']); if(!$u instanceof CustomUserInterface) return $this->get('net15.restifier.dispatch')->error('Operation denied'); //check if actual user is member of the group if(!$this->get('net15.groups.manager')->canUse($group,$actualUser)) return $this->get('net15.restifier.dispatch')->error('Operation denied'); $this->get('net15.groups.manager')->setUserRoleInGroup($group,$u); $retrn['invitation'] = true; } return new JsonResponse($retrn); } public function listUserGroup_GET(){ $u = $this->getUser() ; if(!$u instanceof CustomUserInterface) return $this->get('net15.restifier.dispatch')->error('Operation denied'); $r = $this->get('net15.groups.manager')->getGroupForUser($u); $objectName = $this->get('net15.groups.manager')->getClassName(); if(!is_string($objectName) || empty($objectName)) return $this->get('net15.restifier.dispatch')->error('Internal error'); $retrn = $this->get('net15.restifier.process')->objectToArray($r, ['id','name','created','publicKey','publicVisible'], [ $objectName => ['id','name','created','publicKey','publicVisible'] ] ); return new JsonResponse($retrn); } public function createGroup_POST(){ $data = $this->get('net15.restifier.dispatch')->getUnserialized(); if(!isset($data['group']['name'])) return $this->get('net15.restifier.dispatch')->error('Missing group\'s attributes'); $data['group']['name'] = $this->get('net15.groups.manager')->sanitizeName($data['group']['name']); $g = $this->get('net15.groups.manager')->generateGroupObject($data['group']['name']); if(!$g instanceof CustomUserGroupInterface) return $this->get('net15.restifier.dispatch')->error('Cannot create object'); $r = $this->get('net15.groups.manager')->registerGroup($g); if(!$r instanceof CustomUserGroupInterface) return $this->get('net15.restifier.dispatch')->error('Cannot register object'); $objectName = $this->get('net15.groups.manager')->getClassName(); if(!is_string($objectName) || empty($objectName)) return $this->get('net15.restifier.dispatch')->error('Internal error'); $retrn = $this->get('net15.restifier.process')->objectToArray($r, ['id','name','created','publicKey','publicVisible'] ); return new JsonResponse($retrn); } public function publicAction($publicKey) { $this->get('session')->save(); $this->get('net15.restifier.dispatch')->map('GET',$this,'getGroupInfo_GET',$publicKey); return $this->get('net15.restifier.dispatch')->exec(); } public function getGroupInfo_GET($publicKey){ $group = $this->get('net15.groups.manager')->getByPublicKey($publicKey); if(!$group instanceof CustomUserGroupInterface) return $this->get('net15.restifier.dispatch')->error('Group not found'); if($group->isPublicVisible()){ return $this->returnViewGroup($group);//we return the group } $actualUser = $this->getUser() ; if(!$actualUser instanceof CustomUserInterface) return $this->get('net15.restifier.dispatch')->error('Operation denied'); if($this->get('net15.groups.manager')->canView($group,$actualUser)){ return $this->returnViewGroup($group);//we return the group } return $this->get('net15.restifier.dispatch')->error('Operation denied'); } /** * preformed return * * @param CustomUserGroupInterface|CustomUserGroupInterface[] $group * @return JsonResponse */ public function returnViewGroup($group){ $objectName = $this->get('net15.groups.manager')->getClassName(); if(!is_string($objectName) || empty($objectName)) return $this->get('net15.restifier.dispatch')->error('Internal error'); $retrn = $this->get('net15.restifier.process')->objectToArray($group, ['id','name','created','publicKey','publicVisible'], [ $objectName => ['id','name','created','publicKey','publicVisible'] ] ); return new JsonResponse($retrn); } }