diff --git a/Controller/GrouperGroupsController.php b/Controller/GrouperGroupsController.php index a5e1787..5f7f52d 100644 --- a/Controller/GrouperGroupsController.php +++ b/Controller/GrouperGroupsController.php @@ -329,6 +329,25 @@ public function groupOptin() { $this->set('isTemplateUser', $this->GrouperGroup->isTemplateUser($this->userId)); } + /** + * Create a new Grouper Group via Grouper Template + * Note: This is tighly coupled code to requirements, so view is hardcoded to reflect current reqs. Will need + * to update when reqs change or are updated!!! + * + */ + public function groupCreateTemplateForm() { + if ($this->request->is('post')){ + if(!$this->GrouperGroup->createGroupFromTemplate($this->userId, $this->request->data)){ + $this->Flash->set("Error in creating group!", array('key' => 'error')); + return $this->redirect(array('action' => 'groupoptin')); + } + } + //TODO - not sure if will be able to edit via Templates, so will need to fix once known. + $this->set('title', _txt('pl.grouperlite.title.groupcreate')); + //$this->set('grouperstems', $this->GrouperGroup->getOwnedStems($this->userId)); + } + + //TODO - Need to combine this form with code below for groupCreate public function groupCreateForm() { $this->set('title', _txt('pl.grouperlite.title.groupcreate')); @@ -436,6 +455,7 @@ function isAuthorized() { $p['groupcreate'] = true; $p['joingroup'] = true; $p['leavegroup'] = true; + $p['groupcreatetemplateform'] = true; $this->set('permissions', $p); diff --git a/Lib/GrouperApiAccess.php b/Lib/GrouperApiAccess.php index 7fa0ff4..041ac1a 100644 --- a/Lib/GrouperApiAccess.php +++ b/Lib/GrouperApiAccess.php @@ -417,12 +417,70 @@ public function getGrouperGroupDescription(array $queryData) { return array(); } + /** + * Method used to create a new Group in Grouper via the Template method. + * + * @param array $queryData Array of conditions and data adding new Grouper Group + * @return bool True if added successfully + * @throws GrouperLiteException + * + */ + public function createGroupFromTemplate(array $queryData) { + //Currently only supporting create group, need to test if update a group will work! + + $data = $queryData['data']; + $userId = $queryData['userId']; + + + //Build request logic + $inputFields = array(); + foreach($data as $key => $value) { + $inputFields[] = array('name' => $key, 'value' => $value); + } + + $groupToSave = array( + "WsRestGshTemplateExecRequest" => array( + "gshTemplateActAsSubjectLookup" => array( + "subjectSourceId" => "ldap", + "subjectId" => $userId + ), + "ownerStemLookup" => array( + "stemName" => "ref:incommon-collab" + ), + "ownerType" => "stem", + "configId" => "createNewWorkingGroup", + "inputs" => $inputFields + ) + ); + + echo json_encode($groupToSave); + exit(); + //TODO - take out exit and finish by running an example through + + $this->http->setHeader(array('Content-Type' => 'application/json', 'Accept' => 'application/json')); + $connectionUrl = "{$this->config['fullUrl']}/gshTemplateExec"; + + try { + $results = $this->http->sendRequest('POST', $connectionUrl, json_encode($groupToSave)); + + if (isset($results['WsGshTemplateExecResult']['resultMetadata']['resultCode'])) { + if (stripos($results['WsGshTemplateExecResult']['resultMetadata']['resultCode'], "SUCCESS", 0) !== false) { + return true; + } + } + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': An error occurred'); + throw $e; + } + return false; + } + /** * NOTE: This will probably changes once templates become available to use * * Create or Update a Group where User is Admin/Owner * - * @param array $queryData Array of conditions for querying + * @param array $queryData Array of conditions and data adding new Grouper Group * @return bool True if added or updated successful * @throws GrouperLiteException */ diff --git a/Model/GrouperGroup.php b/Model/GrouperGroup.php index 1ef04ee..b5fa597 100644 --- a/Model/GrouperGroup.php +++ b/Model/GrouperGroup.php @@ -395,6 +395,48 @@ public function optinGroups(array $conditions) { } } + /** + * Create a new Grouper Group using the Template methodology in Grouper + * + * @param string $userId Id of User + * @param array $groupData Data needed to create new Grouper Group via Template + * @return bool True if successfully created record + * @throws GrouperLiteException + * + */ + public function createGroupFromTemplate(string $userId, array $groupData) { + $this->initApi(); + + try { + //Need to massage incoming data to meet Grouper Template requirements + $fields = array( + 'gsh_input_isSympa', + 'gsh_input_isSympaModerated', + 'gsh_input_isOptin', + 'gsh_input_isConfluence', + 'gsh_input_isJira' + ); + // Template does not except true/false, so convert to string and send that way + foreach($fields as $field) { + ($groupData[$field] == '0') ? $groupData[$field] = 'false' : $groupData[$field] = 'true'; + } + + $args = array(); + $args['userId'] = $userId; + $args['data'] = $groupData; + + // Reset Session variable that shows if User is an owner or not + $this->resetUserOwner(); + + return $this->grouperAPI->createGroupFromTemplate($args); + + } catch (Exception $e) { + CakeLog::write('error', __METHOD__ . ': An error occurred'); + throw $e; + } + + } + /** * Creates a new Grouper Group * diff --git a/View/GrouperGroups/groupcreatetemplateform.ctp b/View/GrouperGroups/groupcreatetemplateform.ctp new file mode 100644 index 0000000..a4fb74d --- /dev/null +++ b/View/GrouperGroups/groupcreatetemplateform.ctp @@ -0,0 +1,173 @@ +extend('/GrouperGroups/base'); +$this->Html->addCrumb('Create a Template Group'); + +?> +