James Moger
2014-03-14 8982e6e0738c6991b9a4b864423bd4f75383c7f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//Copyright (C) 2012 The Android Open Source Project
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
 
package com.gitblit.transport.ssh.commands;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
 
import com.gitblit.transport.ssh.CommandMetaData;
import com.gitblit.transport.ssh.IKeyManager;
 
/** Set a user's account settings. **/
@CommandMetaData(name = "set-account", description = "Change an account's settings")
public class SetAccountCommand extends BaseKeyCommand {
 
    private static final String ALL = "ALL";
 
    @Argument(index = 0, required = true, metaVar = "USER", usage = "full name, email-address, ssh username or account id")
    private String user;
 
    @Option(name = "--add-ssh-key", metaVar = "-|KEY", usage = "public keys to add to the account")
    private List<String> addSshKeys = new ArrayList<String>();
 
    @Option(name = "--delete-ssh-key", metaVar = "-|KEY", usage = "public keys to delete from the account")
    private List<String> deleteSshKeys = new ArrayList<String>();
 
    @Override
    public void run() throws IOException, UnloggedFailure {
        validate();
        setAccount();
    }
 
    private void validate() throws UnloggedFailure {
        if (addSshKeys.contains("-") && deleteSshKeys.contains("-")) {
            throw new UnloggedFailure(1, "Only one option may use the stdin");
        }
        if (deleteSshKeys.contains(ALL)) {
            deleteSshKeys = Collections.singletonList(ALL);
        }
    }
 
    private void setAccount() throws IOException, UnloggedFailure {
        addSshKeys = readKeys(addSshKeys);
        if (!addSshKeys.isEmpty()) {
            addSshKeys(addSshKeys);
        }
 
        deleteSshKeys = readKeys(deleteSshKeys);
        if (!deleteSshKeys.isEmpty()) {
            deleteSshKeys(deleteSshKeys);
        }
        authenticator.getKeyCache().invalidate(user);
    }
 
    private void addSshKeys(List<String> sshKeys) throws UnloggedFailure,
            IOException {
        IKeyManager keyManager = authenticator.getKeyManager();
        for (String sshKey : sshKeys) {
            keyManager.addKey(user, sshKey);
        }
    }
 
    private void deleteSshKeys(List<String> sshKeys) {
        IKeyManager keyManager = authenticator.getKeyManager();
        if (sshKeys.contains(ALL)) {
            keyManager.removeAllKeys(user);
        } else {
            for (String sshKey : sshKeys) {
                keyManager.removeKey(user, sshKey);
            }
        }
    }
}