View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
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   * Add a user with optional group membership.
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          // Fetch the admin accessor, who's going to do the work.
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 }