projects_role.codeviewer.desc=Ability to view the source code of the project.
+#------------------------------------------------------------------------------
+#
+# PERMISSION TEMPLATES
+#
+#------------------------------------------------------------------------------
+permission_templates=Permission templates
+permission_template.new_template=New permission template
+permission_template.create_template=Create permission template
+permission_template.delete_confirm_title=Delete permission template
+permission_template.do_you_want_to_delete_template=Are you sure that you want to delete this permission template?
+permission_template.delete_template=Delete permission template
+permission_template.edit_template=Edit permission template
+
+
#------------------------------------------------------------------------------
#
# ERRORS HANDLING
}
}
+ public void updatePermissionTemplate(Long templateId, String templateName, @Nullable String description) {
+ PermissionTemplateDto permissionTemplate = new PermissionTemplateDto()
+ .setId(templateId)
+ .setName(templateName)
+ .setDescription(description)
+ .setUpdatedAt(now());
+ SqlSession session = myBatis.openSession();
+ try {
+ PermissionTemplateMapper mapper = session.getMapper(PermissionTemplateMapper.class);
+ mapper.update(permissionTemplate);
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
public void addUserPermission(Long templateId, Long userId, String permission) {
PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto()
.setTemplateId(templateId)
void insert(PermissionTemplateDto permissionTemplate);
+ void update(PermissionTemplateDto permissionTemplate);
+
void delete(Long templateId);
void deleteUsersPermissions(Long templateId);
VALUES (#{name}, #{description}, #{createdAt}, #{updatedAt})
</insert>
+ <update id="update" parameterType="PermissionTemplate">
+ UPDATE permission_templates
+ SET name = #{name}, description = #{description}, updated_at = #{updatedAt}
+ WHERE id = #{id}
+ </update>
+
<delete id="delete" parameterType="Long">
DELETE FROM permission_templates
WHERE id = #{templateId}
}
@Test
- public void should_permission_template_by_name() throws Exception {
+ public void should_select_permission_template_by_name() throws Exception {
setupData("selectPermissionTemplate");
PermissionTemplateDto permissionTemplate = permissionDao.selectTemplateByName("my template");
assertThat(permissionTemplates).onProperty("description").containsOnly("description1", "description2", "description3");
}
+ @Test
+ public void should_update_permission_template() throws Exception {
+ setupData("updatePermissionTemplate");
+
+ permissionDao.updatePermissionTemplate(1L, "new_name", "new_description");
+
+ checkTable("updatePermissionTemplate", "permission_templates", "id", "name", "description");
+ }
+
@Test
public void should_delete_permission_template() throws Exception {
setupData("deletePermissionTemplate");
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="new_name" description="new_description" created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="2" name="template2" description="description2" created_at="[null]" updated_at="[null]"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="template1" description="description1" created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="2" name="template2" description="description2" created_at="[null]" updated_at="[null]"/>
+</dataset>
\ No newline at end of file
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
-import org.sonar.core.user.*;
+import org.sonar.core.user.PermissionDao;
+import org.sonar.core.user.PermissionTemplateDto;
+import org.sonar.core.user.UserDao;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ServerErrorException;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import java.util.List;
/**
return permissionTemplates;
}
- public PermissionTemplate createPermissionTemplate(String name, String description) {
+ public PermissionTemplate createPermissionTemplate(String name, @Nullable String description) {
PermissionTemplateUpdater.checkUserCredentials();
- checkThatTemplateNameIsUnique(name);
+ validateTemplateName(null, name);
PermissionTemplateDto permissionTemplateDto = permissionDao.createPermissionTemplate(name, description);
if(permissionTemplateDto.getId() == null) {
String errorMsg = "Template creation failed";
return PermissionTemplate.create(permissionTemplateDto);
}
- public void deletePermissionTemplate(String templateName) {
+ public void updatePermissionTemplate(Long templateId, String newName, @Nullable String newDescription) {
PermissionTemplateUpdater.checkUserCredentials();
- PermissionTemplateDto permissionTemplateDto = permissionDao.selectTemplateByName(templateName);
- if(permissionTemplateDto == null) {
- String errorMsg = "Unknown template:" + templateName;
- LOG.error(errorMsg);
- throw new BadRequestException(errorMsg);
- } else {
- permissionDao.deletePermissionTemplate(permissionTemplateDto.getId());
- }
+ validateTemplateName(templateId, newName);
+ permissionDao.updatePermissionTemplate(templateId, newName, newDescription);
+ }
+
+ public void deletePermissionTemplate(Long templateId) {
+ PermissionTemplateUpdater.checkUserCredentials();
+ permissionDao.deletePermissionTemplate(templateId);
}
public void addUserPermission(String templateName, String permission, final String userLogin) {
updater.executeUpdate();
}
- private void checkThatTemplateNameIsUnique(String name) {
+ private void validateTemplateName(Long templateId, String templateName) {
+ if(templateName == null) {
+ String errorMsg = "The name field is mandatory";
+ LOG.error(errorMsg);
+ throw new BadRequestException(errorMsg);
+ }
List<PermissionTemplateDto> existingTemplates = permissionDao.selectAllPermissionTemplates();
if(existingTemplates != null) {
for (PermissionTemplateDto existingTemplate : existingTemplates) {
- if(existingTemplate.getName().equals(name)) {
+ if((templateId == null || templateId != existingTemplate.getId()) && (existingTemplate.getName().equals(templateName))) {
String errorMsg = "A template with that name already exists";
LOG.error(errorMsg);
throw new BadRequestException(errorMsg);
--- /dev/null
+#
+# Sonar, entreprise quality control tool.
+# Copyright (C) 2008-2013 SonarSource
+# mailto:contact AT sonarsource DOT com
+#
+# SonarQube 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.
+#
+# SonarQube 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.
+#
+
+#
+# @since 3.7
+#
+# Note : do NOT use @template as an instance variable
+# as it is a reserved variable in Rails
+#
+class PermissionTemplatesController < ApplicationController
+
+ SECTION=Navigation::SECTION_CONFIGURATION
+
+ before_filter :admin_required
+
+ #
+ # GET
+ #
+ def index
+ @permission_templates = Internal.permission_templates.selectAllPermissionTemplates
+ end
+
+ def edit_users
+ end
+
+ def edit_groups
+ end
+
+ def create_form
+ render :partial => 'permission_templates/permission_template_form',
+ :locals => {:form_action => 'create', :message_title => 'new_template', :message_submit => 'create_template'}
+ end
+
+ #
+ # POST
+ #
+ def create
+ verify_post_request
+ @permission_template = Internal.permission_templates.createPermissionTemplate(params[:name], params[:description])
+ redirect_to :action => 'index'
+ end
+
+ def edit_form
+ @permission_template = Internal.permission_templates.selectPermissionTemplate(params[:name])
+ render :partial => 'permission_templates/permission_template_form',
+ :locals => {:form_action => 'edit', :message_title => 'edit_template', :message_submit => 'edit_template'}
+ end
+
+ #
+ # POST
+ #
+ def edit
+ verify_post_request
+ require_parameters :id, :name
+ Internal.permission_templates.updatePermissionTemplate(params[:id].to_i, params[:name], params[:description])
+ redirect_to :action => 'index'
+ end
+
+ def delete_form
+ @permission_template = Internal.permission_templates.selectPermissionTemplate(params[:name])
+ render :partial => 'permission_templates/delete_form'
+ end
+
+ #
+ # POST
+ #
+ def delete
+ verify_post_request
+ require_parameters :id
+ Internal.permission_templates.deletePermissionTemplate(params[:id].to_i)
+ redirect_to :action => 'index'
+ end
+
+end
--- /dev/null
+<form id="delete-template-form" method="post" action="<%= ApplicationController.root_context -%>/permission_templates/delete">
+ <input type="hidden" name="id" value="<%= @permission_template.id -%>">
+ <fieldset>
+ <div class="modal-head">
+ <h2><%= message 'permission_template.delete_confirm_title' -%></h2>
+ </div>
+ <div class="modal-body">
+ <div id="permission-template-error" class="permission-template error errors" style="display:none;" />
+ <div class="info">
+ <img src="<%= ApplicationController.root_context -%>/images/information.png" style="vertical-align: text-bottom"/>
+ <%= message 'permission_template.do_you_want_to_delete_template' -%>
+ </div>
+ </div>
+ <div class="modal-foot">
+ <input type="submit" value="<%= message 'permission_template.delete_template' -%>" id="confirm-submit"/>
+ <a href="#" onclick="return closeModalWindow()" id="confirm-cancel"><%= h message('cancel') -%></a>
+ </div>
+ </fieldset>
+</form>
+
+<script>
+ $j("#delete-template-form").modalForm({success: function (data) {
+ window.location.reload();
+ }});
+</script>
--- /dev/null
+<form id="permission-template-form" method="post" action="<%= ApplicationController.root_context -%>/permission_templates/<%= form_action %>">
+ <% if form_action == 'edit' %>
+ <input type="hidden" name="id" value="<%= @permission_template.id -%>">
+ <% end %>
+ <fieldset>
+ <div class="modal-head">
+ <h2><%= message("permission_template.#{message_title}") -%></h2>
+ </div>
+ <div class="modal-body">
+ <div id="permission-template-error" class="permission-template error errors" style="display:none;" />
+ <div class="modal-field">
+ <label for="name"><%= h message('name') -%> <em class="mandatory">*</em></label>
+ <input id="name" name="name" type="text" size="50" maxlength="256" value="<%= @permission_template ? @permission_template.name : '' -%>" autofocus="autofocus"/>
+ </div>
+ <div class="modal-field">
+ <label for="description"><%= h message('description') -%></label>
+ <input id="description" name="description" type="text" size="50" maxlength="4000" value="<%= @permission_template ? @permission_template.description : '' -%>"/>
+ </div>
+ </div>
+ <div class="modal-foot">
+ <input type="submit" value="<%= h message("permission_template.#{message_submit}") -%>" id="submit-permission-template-<%= form_action %>"/>
+ <a href="#" onclick="return closeModalWindow()" id="cancel-edit-template"><%= h message('cancel') -%></a>
+ </div>
+ </fieldset>
+</form>
+
+<script>
+ $j("#permission-template-form").modalForm({
+ success: function (data) {
+ window.location.reload();
+ },
+ error: function (xhr, textStatus, errorThrown) {
+ var htmlClass = 'permission-template';
+ $j('input[id^="submit-permission-template"]').removeAttr('disabled');
+ $j('.' + htmlClass + '.errors').show();
+ $j('.' + htmlClass + '.errors').html(xhr.responseText);
+ }
+ });
+</script>
+
--- /dev/null
+<div class="line-block">
+ <ul style="float: right" class="horizontal">
+ <li class="marginleft10 add">
+ <a id="create-link-permission-template" href="permission_templates/create_form" class="open-modal link-action"><%= message('create') -%></a>
+ </li>
+ </ul>
+ <h1><%= message('permission_templates') -%></h1>
+</div>
+
+<table class="data width100" id="permission-templates">
+ <thead>
+ <tr>
+ <th class="left" width="20%"><%= message('name') -%></th>
+ <th class="left" colspan="3"></th>
+ <th class="right" width="5%" colspan="2"><%= message('operations') -%></th>
+ </tr>
+ </thead>
+
+ <tbody>
+ <% if @permission_templates.empty? %>
+ <tr class="even">
+ <td colspan="4" align="left"><%= message('no_results') %></td>
+ </tr>
+ <% end %>
+ <% @permission_templates.each do |permission_template| %>
+ <tr class="<%= cycle('even', 'odd') -%>">
+ <td valign="top"><b><%= h permission_template.name %></b><br/>
+ <span class="small gray"><%= h permission_template.description -%></span>
+ </td>
+ <td valign="top">
+ <%
+ users=Api::Utils.insensitive_sort(permission_template.getUsersForPermission('admin'))
+ groups=Api::Utils.insensitive_sort(permission_template.getGroupsForPermission('admin'))
+ %>
+ <span id="u-admin-<%= u permission_template.name -%>"><%= users.join(', ') %></span>
+ (<a href="<%= ApplicationController.root_context -%>/permission_templates/edit_users?redirect=projects&role=admin&resource=<%= permission_template.id -%>" class="link-action" id="selectu-admin-<%= u permission_template.name -%>">select users</a>)<br/>
+ <span id="g-admin-<%= u permission_template.name -%>"><%= groups.join(', ') %></span>
+ (<a href="<%= ApplicationController.root_context -%>/permission_templates/edit_groups?redirect=projects&role=admin&resource=<%= permission_template.id -%>&q=<%= u params[:q] -%>&qualifier=<%= @qualifier -%>&page=<%= params[:page] -%>" class="link-action" id="selectg-admin-<%= u permission_template.name -%>">select groups</a>)
+ </td>
+ <td valign="top">
+ <%
+ users=Api::Utils.insensitive_sort(permission_template.getUsersForPermission('user'))
+ groups=Api::Utils.insensitive_sort(permission_template.getGroupsForPermission('groups'))
+ %>
+ <span id="u-user-<%= u permission_template.name -%>"><%= users.join(', ') %></span>
+ (<a href="<%= ApplicationController.root_context -%>/roles/edit_users?redirect=projects&role=user&resource=<%= permission_template.id -%>&q=<%= u params[:q] -%>&qualifier=<%= @qualifier -%>&page=<%= params[:page] -%>" class="link-action" id="selectu-user-<%= u permission_template.name -%>">select users</a>)<br/>
+ <span id="g-user-<%= u permission_template.name -%>"><%= groups.join(', ') %></span>
+ (<a href="<%= ApplicationController.root_context -%>/roles/edit_groups?redirect=projects&role=user&resource=<%= permission_template.id -%>&q=<%= u params[:q] -%>&qualifier=<%= @qualifier -%>&page=<%= params[:page] -%>" class="link-action" id="selectg-user-<%= u permission_template.name -%>">select groups</a>)
+ </td>
+ <td valign="top">
+ <%
+ users=Api::Utils.insensitive_sort(permission_template.getUsersForPermission('codeviewer'))
+ groups=Api::Utils.insensitive_sort(permission_template.getGroupsForPermission('codeviewer'))
+ %>
+ <span id="u-codeviewer-<%= u permission_template.name -%>"><%= users.join(', ') %></span>
+ (<a href="<%= ApplicationController.root_context -%>/roles/edit_users?redirect=projects&role=codeviewer&resource=<%= permission_template.id -%>&q=<%= u params[:q] -%>&qualifier=<%= @qualifier -%>&page=<%= params[:page] -%>" class="link-action" id="selectu-codeviewer-<%= u permission_template.name -%>">select users</a>)<br/>
+ <span id="g-codeviewer-<%= u permission_template.name -%>"><%= groups.join(', ') %></span>
+ (<a href="<%= ApplicationController.root_context -%>/roles/edit_groups?redirect=projects&role=codeviewer&resource=<%= permission_template.id -%>&q=<%= u params[:q] -%>&qualifier=<%= @qualifier -%>&page=<%= params[:page] -%>" class="link-action" id="selectg-codeviewer-<%= u permission_template.name -%>">select groups</a>)
+ </td>
+ <td align="right">
+ <%= link_to message('edit'), {:action => :edit_form, :name => permission_template.name},
+ :id => "edit-template-#{u permission_template.name}", :class => 'open-modal link-action' %>
+ </td>
+ <td align="right">
+ <%= link_to message('delete'), {:action => :delete_form, :name => permission_template.name},
+ :id => "delete-template-#{u permission_template.name}", :class => 'open-modal link-action' %>
+ </td>
+ </tr>
+ <%
+ end %>
+ </tbody>
+</table>
public void should_delete_permission_template() throws Exception {
when(permissionDao.selectTemplateByName(DEFAULT_NAME)).thenReturn(DEFAULT_TEMPLATE);
- permissionTemplateService.deletePermissionTemplate(DEFAULT_NAME);
+ permissionTemplateService.deletePermissionTemplate(1L);
verify(permissionDao, times(1)).deletePermissionTemplate(1L);
}
- @Test
- public void should_validate_template_name_on_deletion() throws Exception {
- expected.expect(BadRequestException.class);
- expected.expectMessage("Unknown template:");
-
- when(permissionDao.selectTemplateByName(DEFAULT_NAME)).thenReturn(null);
-
- permissionTemplateService.deletePermissionTemplate(DEFAULT_NAME);
- }
-
@Test
public void should_retrieve_permission_template() throws Exception {
assertThat(templates).onProperty("description").containsOnly("template1", "template2");
}
+ @Test
+ public void should_update_permission_template() throws Exception {
+
+ permissionTemplateService.updatePermissionTemplate(1L, "new_name", "new_description");
+
+ verify(permissionDao).updatePermissionTemplate(1L, "new_name", "new_description");
+ }
+
+ @Test
+ public void should_validate_template_name_on_update_if_applicable() throws Exception {
+ expected.expect(BadRequestException.class);
+ expected.expectMessage("A template with that name already exists");
+
+ PermissionTemplateDto template1 =
+ new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1");
+ PermissionTemplateDto template2 =
+ new PermissionTemplateDto().setId(2L).setName("template2").setDescription("template2");
+ when(permissionDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1, template2));
+
+ permissionTemplateService.updatePermissionTemplate(1L, "template2", "template1");
+ }
+
+ @Test
+ public void should_skip_name_validation_where_not_applicable() throws Exception {
+ PermissionTemplateDto template1 =
+ new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1");
+ PermissionTemplateDto template2 =
+ new PermissionTemplateDto().setId(2L).setName("template2").setDescription("template2");
+ when(permissionDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1, template2));
+
+ permissionTemplateService.updatePermissionTemplate(1L, "template1", "new_description");
+
+ verify(permissionDao).updatePermissionTemplate(1L, "template1", "new_description");
+ }
+
@Test
public void should_add_user_permission() throws Exception {
UserDto userDto = new UserDto().setId(1L).setLogin("user").setName("user");