-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
CreateAction.java
165 lines (147 loc) · 6.84 KB
/
CreateAction.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.user.ws;
import java.util.Collections;
import java.util.List;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
import org.sonar.server.user.ExternalIdentity;
import org.sonar.server.user.NewUser;
import org.sonar.server.user.UserSession;
import org.sonar.server.user.UserUpdater;
import org.sonarqube.ws.WsUsers.CreateWsResponse;
import org.sonarqube.ws.client.user.CreateRequest;
import static com.google.common.base.Strings.emptyToNull;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.user.ExternalIdentity.SQ_AUTHORITY;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_CREATE;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_EMAIL;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_LOCAL;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_LOGIN;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_NAME;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_PASSWORD;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_SCM_ACCOUNT;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_SCM_ACCOUNTS;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_SCM_ACCOUNTS_DEPRECATED;
public class CreateAction implements UsersWsAction {
private final DbClient dbClient;
private final UserUpdater userUpdater;
private final UserSession userSession;
public CreateAction(DbClient dbClient, UserUpdater userUpdater, UserSession userSession) {
this.dbClient = dbClient;
this.userUpdater = userUpdater;
this.userSession = userSession;
}
@Override
public void define(WebService.NewController controller) {
WebService.NewAction action = controller.createAction(ACTION_CREATE)
.setDescription("Create a user.<br/>" +
"If a deactivated user account exists with the given login, it will be reactivated.<br/>" +
"Requires Administer System permission")
.setSince("3.7")
.setChangelog(
new Change("6.3", "The password is only mandatory when creating local users, and should not be set on non local users"),
new Change("6.3", "The 'infos' message is no more returned when a user is reactivated"))
.setPost(true)
.setHandler(this);
action.createParam(PARAM_LOGIN)
.setDescription("User login")
.setRequired(true)
.setExampleValue("myuser");
action.createParam(PARAM_PASSWORD)
.setDescription("User password. Only mandatory when creating local user, otherwise it should not be set")
.setExampleValue("mypassword");
action.createParam(PARAM_NAME)
.setDescription("User name")
.setRequired(true)
.setExampleValue("My Name");
action.createParam(PARAM_EMAIL)
.setDescription("User email")
.setExampleValue("myname@email.com");
action.createParam(PARAM_SCM_ACCOUNTS)
.setDescription("This parameter is deprecated, please use '%s' instead", PARAM_SCM_ACCOUNT)
.setDeprecatedKey(PARAM_SCM_ACCOUNTS_DEPRECATED, "6.0")
.setDeprecatedSince("6.1")
.setExampleValue("myscmaccount1,myscmaccount2");
action.createParam(PARAM_SCM_ACCOUNT)
.setDescription("SCM accounts. To set several values, the parameter must be called once for each value.")
.setExampleValue("scmAccount=firstValue&scmAccount=secondValue&scmAccount=thirdValue");
action.createParam(PARAM_LOCAL)
.setDescription("Specify if the user should be authenticated from SonarQube server or from an external authentication system. " +
"Password should not be set when local is set to false.")
.setSince("6.3")
.setDefaultValue("true")
.setBooleanPossibleValues();
}
@Override
public void handle(Request request, Response response) throws Exception {
userSession.checkLoggedIn().checkIsSystemAdministrator();
writeProtobuf(doHandle(toWsRequest(request)), request, response);
}
private CreateWsResponse doHandle(CreateRequest request) {
try (DbSession dbSession = dbClient.openSession(false)) {
NewUser.Builder newUser = NewUser.builder()
.setLogin(request.getLogin())
.setName(request.getName())
.setEmail(request.getEmail())
.setScmAccounts(request.getScmAccounts())
.setPassword(request.getPassword());
if (!request.isLocal()) {
newUser.setExternalIdentity(new ExternalIdentity(SQ_AUTHORITY, request.getLogin()));
}
UserDto userDto = userUpdater.create(dbSession, newUser.build());
dbSession.commit();
return buildResponse(userDto);
}
}
private static CreateWsResponse buildResponse(UserDto userDto) {
CreateWsResponse.User.Builder userBuilder = CreateWsResponse.User.newBuilder()
.setLogin(userDto.getLogin())
.setName(userDto.getName())
.setActive(userDto.isActive())
.setLocal(userDto.isLocal())
.addAllScmAccounts(userDto.getScmAccountsAsList());
setNullable(emptyToNull(userDto.getEmail()), userBuilder::setEmail);
return CreateWsResponse.newBuilder().setUser(userBuilder).build();
}
private static CreateRequest toWsRequest(Request request) {
return CreateRequest.builder()
.setLogin(request.mandatoryParam(PARAM_LOGIN))
.setPassword(request.param(PARAM_PASSWORD))
.setName(request.param(PARAM_NAME))
.setEmail(request.param(PARAM_EMAIL))
.setScmAccounts(getScmAccounts(request))
.setLocal(request.mandatoryParamAsBoolean(PARAM_LOCAL))
.build();
}
private static List<String> getScmAccounts(Request request) {
if (request.hasParam(PARAM_SCM_ACCOUNT)) {
return request.multiParam(PARAM_SCM_ACCOUNT);
}
List<String> oldScmAccounts = request.paramAsStrings(PARAM_SCM_ACCOUNTS);
return oldScmAccounts != null ? oldScmAccounts : Collections.emptyList();
}
}