]> source.dussan.org Git - sonarqube.git/blob
8c6d6a82ca0e72da7839e98e8223a9e4ec4f9c6f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.permission.ws.template;
21
22 import java.util.Locale;
23 import org.sonar.api.i18n.I18n;
24 import org.sonar.api.server.ws.Request;
25 import org.sonar.api.server.ws.Response;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.api.server.ws.WebService.Param;
28 import org.sonar.core.permission.ProjectPermissions;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.permission.template.PermissionTemplateDto;
33 import org.sonar.server.permission.ws.PermissionWsSupport;
34 import org.sonar.server.permission.ws.PermissionsWsAction;
35 import org.sonar.server.user.UserSession;
36 import org.sonarqube.ws.WsPermissions;
37 import org.sonarqube.ws.WsPermissions.Permission;
38 import org.sonarqube.ws.WsPermissions.PermissionTemplate;
39 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse;
40 import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse.TemplateIdQualifier;
41 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
42
43 import static org.sonar.api.utils.DateUtils.formatDateTime;
44 import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
45 import static org.sonar.server.ws.WsUtils.writeProtobuf;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
47
48 public class SearchTemplatesAction implements PermissionsWsAction {
49   private static final String PROPERTY_PREFIX = "projects_role.";
50   private static final String DESCRIPTION_SUFFIX = ".desc";
51
52   private final DbClient dbClient;
53   private final UserSession userSession;
54   private final I18n i18n;
55   private final PermissionWsSupport support;
56   private final SearchTemplatesDataLoader dataLoader;
57
58   public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, PermissionWsSupport support, SearchTemplatesDataLoader dataLoader) {
59     this.dbClient = dbClient;
60     this.userSession = userSession;
61     this.i18n = i18n;
62     this.support = support;
63     this.dataLoader = dataLoader;
64   }
65
66   @Override
67   public void define(WebService.NewController context) {
68     WebService.NewAction action = context.createAction("search_templates")
69       .setDescription("List permission templates.<br />" +
70         "It requires to be authenticated.")
71       .setResponseExample(getClass().getResource("search_templates-example.json"))
72       .setSince("5.2")
73       .addSearchQuery("defau", "permission template names")
74       .setHandler(this);
75
76     createOrganizationParameter(action);
77   }
78
79   @Override
80   public void handle(Request wsRequest, Response wsResponse) throws Exception {
81     userSession.checkLoggedIn();
82
83     try (DbSession dbSession = dbClient.openSession(false)) {
84       OrganizationDto org = support.findOrganization(dbSession, wsRequest.param(PARAM_ORGANIZATION_KEY));
85       SearchTemplatesWsRequest request = new SearchTemplatesWsRequest()
86         .setOrganizationUuid(org.getUuid())
87         .setQuery(wsRequest.param(Param.TEXT_QUERY));
88
89       SearchTemplatesWsResponse searchTemplatesWsResponse = buildResponse(dataLoader.load(dbSession, request));
90       writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
91     }
92   }
93
94   private WsPermissions.SearchTemplatesWsResponse buildResponse(SearchTemplatesData data) {
95     SearchTemplatesWsResponse.Builder response = SearchTemplatesWsResponse.newBuilder();
96
97     buildTemplatesResponse(response, data);
98     buildDefaultTemplatesResponse(response, data);
99     buildPermissionsResponse(response);
100
101     return response.build();
102   }
103
104   private static void buildDefaultTemplatesResponse(SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
105     TemplateIdQualifier.Builder templateUuidQualifierBuilder = TemplateIdQualifier.newBuilder();
106     for (DefaultPermissionTemplateFinder.TemplateUuidQualifier templateUuidQualifier : data.defaultTemplates()) {
107       response.addDefaultTemplates(templateUuidQualifierBuilder
108         .clear()
109         .setQualifier(templateUuidQualifier.getQualifier())
110         .setTemplateId(templateUuidQualifier.getTemplateUuid()));
111     }
112   }
113
114   private static void buildTemplatesResponse(WsPermissions.SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
115     Permission.Builder permissionResponse = Permission.newBuilder();
116     PermissionTemplate.Builder templateBuilder = PermissionTemplate.newBuilder();
117
118     for (PermissionTemplateDto templateDto : data.templates()) {
119       templateBuilder
120         .clear()
121         .setId(templateDto.getUuid())
122         .setName(templateDto.getName())
123         .setCreatedAt(formatDateTime(templateDto.getCreatedAt()))
124         .setUpdatedAt(formatDateTime(templateDto.getUpdatedAt()));
125       if (templateDto.getKeyPattern() != null) {
126         templateBuilder.setProjectKeyPattern(templateDto.getKeyPattern());
127       }
128       if (templateDto.getDescription() != null) {
129         templateBuilder.setDescription(templateDto.getDescription());
130       }
131       for (String permission : ProjectPermissions.ALL) {
132         templateBuilder.addPermissions(
133           permissionResponse
134             .clear()
135             .setKey(permission)
136             .setUsersCount(data.userCount(templateDto.getId(), permission))
137             .setGroupsCount(data.groupCount(templateDto.getId(), permission))
138             .setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission)));
139       }
140       response.addPermissionTemplates(templateBuilder);
141     }
142   }
143
144   private void buildPermissionsResponse(SearchTemplatesWsResponse.Builder response) {
145     Permission.Builder permissionResponse = Permission.newBuilder();
146     for (String permissionKey : ProjectPermissions.ALL) {
147       response.addPermissions(
148         permissionResponse
149           .clear()
150           .setKey(permissionKey)
151           .setName(i18nName(permissionKey))
152           .setDescription(i18nDescriptionMessage(permissionKey)));
153     }
154   }
155
156   private String i18nDescriptionMessage(String permissionKey) {
157     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey + DESCRIPTION_SUFFIX, "");
158   }
159
160   private String i18nName(String permissionKey) {
161     return i18n.message(Locale.ENGLISH, PROPERTY_PREFIX + permissionKey, permissionKey);
162   }
163 }