-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GetGroupIDBYName and AddGroupMembers options (#23)
* Added GetGroupIDBYName and AddGroupMembers options * Modified README.md for new commands * Removed GetGroupIDByName. Corrections AddGroupMembers and README * Corrections for print and error messages in AddGroupMembers * Corrections to AddGroupMembers
- Loading branch information
1 parent
2b8abc5
commit b952ac7
Showing
2 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/groovy/org/craftercms/cli/commands/group/AddGroupMembers.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.craftercms.cli.commands.group | ||
|
||
import org.craftercms.cli.commands.AbstractCommand | ||
import picocli.CommandLine | ||
|
||
@CommandLine.Command(name = 'add-group-members', description = 'Adds one or more users to a group') | ||
class AddGroupMembers extends AbstractCommand { | ||
|
||
@CommandLine.Spec | ||
CommandLine.Model.CommandSpec commandSpec | ||
|
||
@CommandLine.Option(names = ['-gn', '--group-name'], required = true, description = 'Group name. Required if not bulk importing from a file.') | ||
String groupName | ||
|
||
@CommandLine.Option(names = ['-u', '--users'], split = ',', required = true, description = 'Comma separated list of users to add') | ||
String[] users | ||
|
||
def validateParameters() { | ||
if (!groupName) { | ||
throw new CommandLine.ParameterException(commandSpec.commandLine(), 'Missing required option group-name') | ||
} | ||
|
||
if (!users) { | ||
throw new CommandLine.ParameterException(commandSpec.commandLine(), 'Missing required option users') | ||
} | ||
} | ||
|
||
@Override | ||
def run(client) { | ||
validateParameters() | ||
addMembers(client, groupName, users) | ||
} | ||
|
||
/** | ||
* Add one or more users to a group | ||
* @param client HTTPClient object | ||
* @param groupName group name | ||
* @param users list of users to add | ||
*/ | ||
static def addMembers(client, groupName, users) { | ||
def response = getGroupIdByName(client, groupName) | ||
|
||
if (!response || !response.group) { | ||
println "Error retrieving group '${groupName}'" | ||
return | ||
} | ||
|
||
def groupId = response.group.id | ||
def path = "/studio/api/2/groups/${groupId}/members" | ||
def body = [usernames: users] | ||
|
||
def result = client.post(path, body) | ||
if (!result) { | ||
println "Failed to add members '${users}' to group '${groupName}'" | ||
return | ||
} | ||
|
||
println "Members '${users}' added to group '${groupName}' succesfully" | ||
} | ||
|
||
/** | ||
* Get a group by name | ||
* @param client HTTPClient object | ||
* @param groupName group name | ||
*/ | ||
static def getGroupIdByName(client, groupName) { | ||
def path = "/studio/api/2/groups/by_name/${groupName}" | ||
def result = client.get(path) | ||
if (!result) { | ||
println "Failed to retrieve group '${groupName}'" | ||
return | ||
} | ||
|
||
if (!result.group) { | ||
println "Failed to retrieve group '${groupName}', error '${result.message}'" | ||
return | ||
} | ||
|
||
return result | ||
} | ||
} |