Skip to content

Commit

Permalink
feat: added ssc user create command
Browse files Browse the repository at this point in the history
added ssc user create command to create local ssc users
  • Loading branch information
psmf22 committed Aug 8, 2023
1 parent 4548334 commit 7acc319
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@Command(
name = "user",
subcommands = {
SSCUserCreateCommand.class,
SSCUserDeleteCommand.class,
SSCUserGetCommand.class,
SSCUserListCommand.class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright 2021, 2023 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.ssc.user.cli.cmd;

import java.util.ArrayList;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.ssc._common.output.cli.cmd.AbstractSSCJsonNodeOutputCommand;
import com.fortify.cli.ssc._common.rest.SSCUrls;
import com.fortify.cli.ssc.user.helper.SSCRoleObject;
import com.fortify.cli.ssc.user.helper.SSCUserCreateRequest;

import kong.unirest.UnirestInstance;
import lombok.Getter;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;

@Command(name = OutputHelperMixins.Create.CMD_NAME)
public class SSCUserCreateCommand extends AbstractSSCJsonNodeOutputCommand {
@Getter @Mixin private OutputHelperMixins.Get outputHelper;
@Getter private static ObjectMapper objectMapper = new ObjectMapper();

@Option(names = {"--username"}, required = true)
private String username;
@Option(names = {"--password"}, required = true)
private String password;
@Option(names = {"--firstname"})
private String firstName;
@Option(names = {"--lastname"})
private String lastName;
@Option(names = {"--email"})
private String email;
@ArgGroup(exclusive=true)
Exclusives exclusives = new Exclusives();
@Option(names = {"--roles"}, required = false, split = ",", descriptionKey = "fcli.ssc.role.resolver.nameOrId")
private ArrayList<String> roles = new ArrayList<String>();

@Override
public JsonNode getJsonNode(UnirestInstance unirest) {
ArrayList<SSCRoleObject> roleObjects = new ArrayList<SSCRoleObject>();
roles.forEach((n) -> roleObjects.add(new SSCRoleObject(n)));
SSCUserCreateRequest userCreateRequest = SSCUserCreateRequest.builder()
.userName(username)
.clearPassword(password)
.firstName(firstName)
.lastName(lastName)
.email(email)
.passwordNeverExpires(exclusives.pwNeverExpires)
.requirePasswordChange(exclusives.requirePwChange)
.suspended(exclusives.suspend)
.roles(roleObjects)
.build();
ObjectNode body = objectMapper.valueToTree(userCreateRequest);

return unirest.post(SSCUrls.LOCAL_USERS)
.body(body).asObject(JsonNode.class).getBody();
}

@Override
public boolean isSingular() {
return true;
}

private static class Exclusives {
@Option(names = {"--password-never-expires", "-pne"})
private boolean pwNeverExpires;
@Option(names = {"--require-password-change", "-rpc"})
private boolean requirePwChange;
@Option(names = {"--suspend"})
private boolean suspend;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright 2021, 2023 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.ssc.user.helper;

import lombok.Getter;

public class SSCRoleObject {
@Getter private String id;
public SSCRoleObject(String id) {
this.id = id;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright 2021, 2023 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*******************************************************************************/
package com.fortify.cli.ssc.user.helper;

import java.util.ArrayList;

import com.formkiq.graalvm.annotations.Reflectable;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Reflectable @NoArgsConstructor @AllArgsConstructor
@Data @Builder
public class SSCUserCreateRequest {
private String userName;
private String clearPassword;
private String email;
private String firstName;
private String lastName;
private ArrayList<SSCRoleObject> roles;
@Builder.Default
private Boolean passwordNeverExpires = false;
@Builder.Default
private Boolean requirePasswordChange = false;
@Builder.Default
private Boolean suspended = false;
}

Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ fcli.ssc.token-definition.list.usage.header = List token definitions.

# fcli ssc user
fcli.ssc.user.usage.header = Manage SSC users.
fcli.ssc.user.create.usage.header = Create a local SSC User
fcli.ssc.user.delete.usage.header = Delete a user.
fcli.ssc.user.get.usage.header = Get user details.
fcli.ssc.user.list.usage.header = List users.
Expand Down

0 comments on commit 7acc319

Please sign in to comment.