aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-07-08 11:00:04 +0200
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-07-08 11:00:04 +0200
commit8ca0f28d2bffeb8e8ea3212d84e89685c2d19842 (patch)
tree6cc13786dbb576e715f211ef5420a6ca4674b7f2
parent1ace6fef25bfce62a9b96a3a079e80cb75f819a7 (diff)
downloadsonarqube-8ca0f28d2bffeb8e8ea3212d84e89685c2d19842.tar.gz
sonarqube-8ca0f28d2bffeb8e8ea3212d84e89685c2d19842.zip
SONAR-4466 Added controller and pages to handle CRUD operations on permission templates
-rw-r--r--plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties14
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java16
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java2
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml6
-rw-r--r--sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java11
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate-result.xml4
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate.xml4
-rw-r--r--sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java35
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/permission_templates_controller.rb91
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_delete_form.html.erb25
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_permission_template_form.html.erb40
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/index.html.erb72
-rw-r--r--sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java47
13 files changed, 341 insertions, 26 deletions
diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
index 74ff3af35cb..203475dae72 100644
--- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
+++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties
@@ -2244,6 +2244,20 @@ 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
#
#------------------------------------------------------------------------------
diff --git a/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java b/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java
index 14deada2ff3..2d1e499c6fc 100644
--- a/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java
+++ b/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java
@@ -110,6 +110,22 @@ public class PermissionDao implements TaskExtension, ServerExtension {
}
}
+ 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)
diff --git a/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java b/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java
index f3f3f85f884..124492f07a5 100644
--- a/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java
+++ b/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java
@@ -27,6 +27,8 @@ public interface PermissionTemplateMapper {
void insert(PermissionTemplateDto permissionTemplate);
+ void update(PermissionTemplateDto permissionTemplate);
+
void delete(Long templateId);
void deleteUsersPermissions(Long templateId);
diff --git a/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml b/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml
index 0cabe293958..d07c5232aab 100644
--- a/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml
+++ b/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml
@@ -8,6 +8,12 @@
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}
diff --git a/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java
index de5aef22b0a..dba245bb5a8 100644
--- a/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java
@@ -75,7 +75,7 @@ public class PermissionDaoTest extends AbstractDaoTestCase {
}
@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");
@@ -99,6 +99,15 @@ public class PermissionDaoTest extends AbstractDaoTestCase {
}
@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");
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate-result.xml
new file mode 100644
index 00000000000..c29fef833b8
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate-result.xml
@@ -0,0 +1,4 @@
+<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate.xml
new file mode 100644
index 00000000000..ef9d2a209af
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/updatePermissionTemplate.xml
@@ -0,0 +1,4 @@
+<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
diff --git a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java
index 7854a6bd12a..f56429a6861 100644
--- a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java
+++ b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java
@@ -24,11 +24,14 @@ import com.google.common.collect.Lists;
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;
/**
@@ -65,9 +68,9 @@ public class InternalPermissionTemplateService implements ServerComponent {
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";
@@ -77,16 +80,15 @@ public class InternalPermissionTemplateService implements ServerComponent {
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) {
@@ -133,11 +135,16 @@ public class InternalPermissionTemplateService implements ServerComponent {
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);
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/permission_templates_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/permission_templates_controller.rb
new file mode 100644
index 00000000000..60b25cdb82f
--- /dev/null
+++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/permission_templates_controller.rb
@@ -0,0 +1,91 @@
+#
+# 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
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_delete_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_delete_form.html.erb
new file mode 100644
index 00000000000..e373a37142c
--- /dev/null
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_delete_form.html.erb
@@ -0,0 +1,25 @@
+<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>
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_permission_template_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_permission_template_form.html.erb
new file mode 100644
index 00000000000..a2203f7f1f8
--- /dev/null
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_permission_template_form.html.erb
@@ -0,0 +1,40 @@
+<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>
+
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/index.html.erb
new file mode 100644
index 00000000000..9c20a5811b5
--- /dev/null
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/index.html.erb
@@ -0,0 +1,72 @@
+<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>
diff --git a/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java b/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java
index 5ade294c424..a119ce86cc8 100644
--- a/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java
@@ -83,22 +83,12 @@ public class InternalPermissionTemplateServiceTest {
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 {
List<PermissionTemplateUserDto> usersPermissions = Lists.newArrayList(
@@ -150,6 +140,41 @@ public class InternalPermissionTemplateServiceTest {
}
@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");
when(userDao.selectActiveUserByLogin("user")).thenReturn(userDto);