1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.commands;
21
22 import com.google.common.collect.Maps;
23 import com.tinkerpop.frames.FramedGraph;
24 import eu.ehri.project.acl.SystemScope;
25 import eu.ehri.project.core.GraphManager;
26 import eu.ehri.project.core.GraphManagerFactory;
27 import eu.ehri.project.definitions.Ontology;
28 import eu.ehri.project.exceptions.DeserializationError;
29 import eu.ehri.project.exceptions.ItemNotFound;
30 import eu.ehri.project.exceptions.PermissionDenied;
31 import eu.ehri.project.exceptions.ValidationError;
32 import eu.ehri.project.models.EntityClass;
33 import eu.ehri.project.models.Group;
34 import eu.ehri.project.models.UserProfile;
35 import eu.ehri.project.models.base.Accessor;
36 import eu.ehri.project.persistence.Bundle;
37 import eu.ehri.project.api.Api;
38 import org.apache.commons.cli.CommandLine;
39 import org.apache.commons.cli.Option;
40 import org.apache.commons.cli.Options;
41
42
43
44
45 public class UserAdd extends BaseCommand {
46
47 final static String NAME = "useradd";
48
49 @Override
50 protected void setCustomOptions(Options options) {
51 options.addOption(Option.builder()
52 .longOpt("group")
53 .hasArg()
54 .type(String.class)
55 .desc("A group to add the user to")
56 .build());
57 options.addOption(Option.builder("n")
58 .longOpt("name")
59 .hasArg()
60 .type(String.class)
61 .desc("User's full name")
62 .build());
63 options.addOption(Option.builder()
64 .longOpt("log")
65 .hasArg()
66 .type(String.class)
67 .desc("Log message for update action.")
68 .build());
69 }
70
71 @Override
72 public String getUsage() {
73 return String.format("%s [OPTIONS] <user-identifier>", NAME);
74 }
75
76 @Override
77 public String getHelp() {
78 return "Create a new user, and optionally add them to a group";
79 }
80
81 @Override
82 public int execWithOptions(FramedGraph<?> graph,
83 CommandLine cmdLine) throws ItemNotFound, ValidationError, PermissionDenied, DeserializationError {
84
85 GraphManager manager = GraphManagerFactory.getInstance(graph);
86 String logMessage = cmdLine.getOptionValue("c",
87 "Created via command-line");
88
89 if (cmdLine.getArgList().size() < 1)
90 throw new RuntimeException(getUsage());
91
92
93 Accessor admin = manager.getEntity(Group.ADMIN_GROUP_IDENTIFIER,
94 Accessor.class);
95
96 String userId = cmdLine.getArgList().get(0);
97 String userName = cmdLine.getOptionValue("n", userId);
98 String[] groups = {};
99 if (cmdLine.hasOption("group")) {
100 groups = cmdLine.getOptionValues("group");
101 }
102
103 Bundle bundle = Bundle.of(EntityClass.USER_PROFILE,
104 Maps.<String, Object>newHashMap())
105 .withDataValue(Ontology.IDENTIFIER_KEY, userId)
106 .withDataValue(Ontology.NAME_KEY, userName);
107 String nodeId = EntityClass.USER_PROFILE.getIdGen()
108 .generateId(SystemScope.getInstance().idPath(), bundle);
109 bundle = bundle.withId(nodeId);
110
111 Api api = api(graph, admin);
112 UserProfile newUser = api
113 .create(bundle, UserProfile.class, getLogMessage(logMessage));
114 for (String groupId : groups) {
115 Group group = manager.getEntity(groupId, EntityClass.GROUP, Group.class);
116 api.acl().addAccessorToGroup(group, newUser);
117 }
118
119 return 0;
120 }
121 }