View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.util.shell.commands;
18  
19  import java.io.IOException;
20  
21  import org.apache.accumulo.core.client.AccumuloException;
22  import org.apache.accumulo.core.client.AccumuloSecurityException;
23  import org.apache.accumulo.core.security.SystemPermission;
24  import org.apache.accumulo.core.security.TablePermission;
25  import org.apache.accumulo.core.util.shell.Shell;
26  import org.apache.accumulo.core.util.shell.Shell.Command;
27  import org.apache.commons.cli.CommandLine;
28  import org.apache.commons.cli.Option;
29  import org.apache.commons.cli.Options;
30  
31  public class UserPermissionsCommand extends Command {
32    private Option userOpt;
33    private static int runOnce = 0;
34    
35    @Override
36    public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, IOException {
37      final String user = cl.getOptionValue(userOpt.getOpt(), shellState.getConnector().whoami());
38      
39      String delim = "";
40      shellState.getReader().printString("System permissions: ");
41      for (SystemPermission p : SystemPermission.values()) {
42        if (p != null && shellState.getConnector().securityOperations().hasSystemPermission(user, p)) {
43          shellState.getReader().printString(delim + "System." + p.name());
44          delim = ", ";
45        }
46      }
47      shellState.getReader().printNewline();
48      
49      for (String t : shellState.getConnector().tableOperations().list()) {
50        delim = "";
51        for (TablePermission p : TablePermission.values()) {
52          if (shellState.getConnector().securityOperations().hasTablePermission(user, t, p) && p != null) {
53            if (runOnce == 0) {
54              shellState.getReader().printString("\nTable permissions (" + t + "): ");
55              runOnce++;
56            }
57            shellState.getReader().printString(delim + "Table." + p.name());
58            delim = ", ";
59          }
60          
61        }
62        runOnce = 0;
63      }
64      shellState.getReader().printNewline();
65      return 0;
66    }
67    
68    @Override
69    public String description() {
70      return "displays a user's system and table permissions";
71    }
72    
73    @Override
74    public Options getOptions() {
75      Options o = new Options();
76      userOpt = new Option(Shell.userOption, "user", true, "user to operate on");
77      userOpt.setArgName("user");
78      o.addOption(userOpt);
79      return o;
80    }
81    
82    @Override
83    public int numArgs() {
84      return 0;
85    }
86  }