Can get group settings.

This commit is contained in:
Pierre HUBERT 2018-07-04 07:44:37 +02:00
parent 291558578b
commit 57401c8ce0
3 changed files with 123 additions and 5 deletions

View File

@ -59,7 +59,7 @@ class GroupsController {
public function getInfo(){
//Get the ID of the requested group
$id = postInt("id");
$id = getPostGroupId("id");
//Get information about the group
$group = components()->groups->get_info($id);
@ -80,7 +80,7 @@ class GroupsController {
public function getAdvancedInfo(){
//Get the ID of the requested group
$id = postInt("id");
$id = getPostGroupId("id");
//Get information about the group
$group = components()->groups->get_advanced_info($id);
@ -93,6 +93,49 @@ class GroupsController {
return self::AdvancedGroupInfoToAPI($group);
}
/**
* Get the settings of a group
*
* @url POST /groups/get_settings
*/
public function getSettings(){
//Get the ID of the group (with admin access)
$groupID = $this->getPostGroupIDWithAdmin("id");
//Retrieve the settings of the group
$settings = components()->groups->get_settings($groupID);
//Check for error
if(!$settings->isValid())
Rest_fatal_error(500, "Could not get the settings of the group!");
//Return parsed settings
return self::GroupSettingsToAPI($settings);
}
/**
* Get and return a group ID specified in the POST request
* in which the current user has admin rigths
*
* @param string $name The name of the POST field
* @return int The ID of the group
*/
private function getPostGroupIDWithAdmin(string $name) : int {
//User must be signed in
user_login_required();
//Get the ID of the group
$groupID = getPostGroupId($name);
//Check if the user is an admin of the group or not
if(!components()->groups->isAdmin(userID, $groupID))
Rest_fatal_error(401, "You are not an administrator of this group!");
return $groupID;
}
/**
* Parse a GroupInfo object into an array for the API
*
@ -124,4 +167,16 @@ class GroupsController {
return $data;
}
/**
* Parse a GroupSettings object into an array for the API
*
* @param GroupSettings $settings The settings to parse
* @return array Generated array
*/
public static function GroupSettingsToAPI(GroupSettings $info) : array {
$data = self::AdvancedGroupInfoToAPI($info);
return $data;
}
}

View File

@ -107,6 +107,27 @@ class GroupsComponent {
return $this->dbToAdvancedGroupInfo($info[0]);
}
/**
* Get a group settings
*
* @param int $id The ID of the target group
* @return GroupSettings The settings of the group / invalid
* GroupSettings object in case of failure
*/
public function get_settings(int $id) : GroupSettings {
//Query the database
$info = db()->select(self::GROUPS_LIST_TABLE, "WHERE id = ?", array($id));
//Check for results
if(count($info) == 0)
return new GroupSettings(); //Return invalid object
//Create and fill GroupInfo object with database entry
return $this->dbToGroupSettings($info[0]);
}
/**
* Insert a new group member
*
@ -164,6 +185,18 @@ class GroupsComponent {
return $results[0]["level"];
}
/**
* Check whether a user is an administrator of a group
* or not
*
* @param int $userID Requested user ID to check
* @return bool TRUE if the user is an admin / FALSE else
*/
public function isAdmin(int $userID, int $groupID){
return $this->getMembershipLevel($userID, $groupID)
== GroupMember::ADMINISTRATOR;
}
/**
* Count the number of members of a group
*
@ -198,15 +231,19 @@ class GroupsComponent {
}
/**
* Turn a database entry into AdvancedGroupInfo object entry
* Turn a database group entry into AdvancedGroupInfo object entry
*
* @param array $data Database entry
* @param AdvancedGroupInfo $info Optionnal, fill an existing object
* instead of creating a new one
* @return AdvancedGroupInfo Advanced information about the group
*/
private function dbToAdvancedGroupInfo(array $data) : AdvancedGroupInfo {
private function dbToAdvancedGroupInfo(array $data, AdvancedGroupInfo $info = null) : AdvancedGroupInfo {
if($info == null)
$info = new AdvancedGroupInfo();
//Parse basical information about the group
$info = new AdvancedGroupInfo();
$this->dbToGroupInfo($data, $info);
//Parse advanced information
@ -215,6 +252,22 @@ class GroupsComponent {
return $info;
}
/**
* Turn a database group entry into GroupSettings object
*
* @param array $data Database entry
* @return GroupSettings The settings of the group
*/
private function dbToGroupSettings(array $data) : GroupSettings {
//Parse advanced settings about the group
$info = new GroupSettings();
$this->dbToAdvancedGroupInfo($data, $info);
return $info;
}
}
//Register component

View File

@ -0,0 +1,10 @@
<?php
/**
* Group settings model object
*
* @author Pierre HUBERT
*/
class GroupSettings extends AdvancedGroupInfo {
}