return allGlobal;
}
+ public static boolean isValid(String permission) {
+ return allGlobal.containsKey(permission);
+ }
}
this.myBatis = myBatis;
}
+ public PermissionTemplateDto selectTemplateByName(String templateName) {
+ SqlSession session = myBatis.openSession();
+ try {
+ PermissionTemplateMapper mapper = session.getMapper(PermissionTemplateMapper.class);
+ return mapper.selectByName(templateName);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
public PermissionTemplateDto selectPermissionTemplate(String templateName) {
PermissionTemplateDto permissionTemplate = null;
SqlSession session = myBatis.openSession();
return permissionTemplate;
}
+ public void deletePermissionTemplate(Long templateId) {
+ SqlSession session = myBatis.openSession();
+ try {
+ PermissionTemplateMapper mapper = session.getMapper(PermissionTemplateMapper.class);
+ mapper.deleteUsersPermissions(templateId);
+ mapper.deleteGroupsPermissions(templateId);
+ mapper.delete(templateId);
+ 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 delete(Long templateId);
+
+ void deleteUsersPermissions(Long templateId);
+
+ void deleteGroupsPermissions(Long templateId);
+
+ PermissionTemplateDto selectByName(String templateName);
+
PermissionTemplateDto selectTemplateUsersPermissions(String templateName);
PermissionTemplateDto selectTemplateGroupsPermissions(String templateName);
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"USER_ID" INTEGER NOT NULL,
"TEMPLATE_ID" INTEGER NOT NULL,
- "PERMISSION" VARCHAR(64) NOT NULL,
+ "PERMISSION_REFERENCE" VARCHAR(64) NOT NULL,
"CREATED_AT" TIMESTAMP,
"UPDATED_AT" TIMESTAMP
);
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
"GROUP_ID" INTEGER,
"TEMPLATE_ID" INTEGER NOT NULL,
- "PERMISSION" VARCHAR(64) NOT NULL,
+ "PERMISSION_REFERENCE" VARCHAR(64) NOT NULL,
"CREATED_AT" TIMESTAMP,
"UPDATED_AT" TIMESTAMP
);
VALUES (#{name}, #{description}, #{createdAt}, #{updatedAt})
</insert>
+ <delete id="delete" parameterType="Long">
+ DELETE FROM permission_templates
+ WHERE id = #{templateId}
+ </delete>
+
+ <delete id="deleteUsersPermissions" parameterType="Long">
+ DELETE FROM perm_templates_users
+ WHERE template_id = #{templateId}
+ </delete>
+
+ <delete id="deleteGroupsPermissions" parameterType="Long">
+ DELETE FROM perm_templates_groups
+ WHERE template_id = #{templateId}
+ </delete>
+
<insert id="insertUserPermission" parameterType="PermissionTemplateUser">
- INSERT INTO perm_templates_users (template_id, user_id, permission, created_at, updated_at)
+ INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
</insert>
</delete>
<insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
- INSERT INTO perm_templates_groups (template_id, group_id, permission, created_at, updated_at)
+ INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
VALUES (#{templateId}, #{groupId}, #{permission}, #{createdAt}, #{updatedAt})
</insert>
</choose>
</delete>
+ <select id="selectByName" parameterType="String" resultType="PermissionTemplate">
+ SELECT id, name, description, created_at AS createdAt, updated_at AS updatedAt
+ FROM permission_templates
+ WHERE name = #{templateName}
+ </select>
+
<select id="selectAllPermissionTemplates" resultType="PermissionTemplate">
SELECT id, name, description, created_at AS createdAt, updated_at AS updatedAt
FROM permission_templates
pt.created_at AS template_created_at,
pt.updated_at AS template_updated_at,
ptu.id AS permission_template_user_id,
- ptu.permission AS user_permission,
+ ptu.permission_reference AS user_permission,
u.name AS user_name
FROM permission_templates AS pt
INNER JOIN perm_templates_users AS ptu ON ptu.template_id = pt.id
pt.created_at AS template_created_at,
pt.updated_at AS template_updated_at,
ptg.id AS permission_template_group_id,
- ptg.permission AS group_permission,
+ ptg.permission_reference AS group_permission,
g.name AS group_name
FROM permission_templates AS pt
INNER JOIN perm_templates_groups AS ptg ON ptg.template_id = pt.id
</select>
<resultMap id="fullPermissionsTemplateResult" type="PermissionTemplate">
- <id property="id" column="template_id" />
+ <id property="id" column="template_id"/>
<result property="name" column="template_name"/>
<result property="description" column="template_description"/>
<result property="createdAt" column="template_created_at"/>
assertThat(permissionTemplate.getGroupsPermissions()).onProperty("permission").containsOnly("group_permission1", "group_permission1", "group_permission2");
}
+ @Test
+ public void should_permission_template_by_name() throws Exception {
+ setupData("selectPermissionTemplate");
+
+ PermissionTemplateDto permissionTemplate = permissionDao.selectTemplateByName("my template");
+
+ assertThat(permissionTemplate).isNotNull();
+ assertThat(permissionTemplate.getId()).isEqualTo(1L);
+ assertThat(permissionTemplate.getName()).isEqualTo("my template");
+ assertThat(permissionTemplate.getDescription()).isEqualTo("my description");
+ }
+
@Test
public void should_select_all_permission_templates() throws Exception {
setupData("selectAllPermissionTemplates");
assertThat(permissionTemplates).onProperty("description").containsOnly("description1", "description2", "description3");
}
+ @Test
+ public void should_delete_permission_template() throws Exception {
+ setupData("deletePermissionTemplate");
+
+ permissionDao.deletePermissionTemplate(1L);
+
+ checkTable("deletePermissionTemplate", "permission_templates", "id", "name", "description");
+ checkTable("deletePermissionTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
+ checkTable("deletePermissionTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+ }
+
@Test
public void should_add_user_permission_to_template() throws Exception {
setupData("addUserPermissionToTemplate");
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
<perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="1" permission="new_permission"/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="new_permission"/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
<perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="[null]" permission="new_permission"/>
+ <perm_templates_groups id="1" template_id="1" group_id="[null]" permission_reference="new_permission"/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
- <perm_templates_users id="1" template_id="1" user_id="1" permission="new_permission"/>
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="new_permission"/>
<perm_templates_groups/>
</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="2" name="other template" description="other description" created_at="[null]" updated_at="[null]"/>
+
+ <perm_templates_users/>
+
+ <perm_templates_groups/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" description="my description" created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="2" name="other template" description="other description" created_at="[null]" updated_at="[null]"/>
+
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
+ <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
+ <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
+
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
+ <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
+ <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
+</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
<perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
<perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
- <perm_templates_groups id="2" template_id="1" group_id="2" permission="existing_permission"/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
+ <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="existing_permission"/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
<perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
<perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
- <perm_templates_groups id="2" template_id="1" group_id="[null]" permission="existing_permission"/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
+ <perm_templates_groups id="2" template_id="1" group_id="[null]" permission_reference="existing_permission"/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
- <perm_templates_users id="1" template_id="1" user_id="1" permission="remaining_permission"/>
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission"/>
<perm_templates_groups/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description"/>
- <perm_templates_users id="1" template_id="1" user_id="1" permission="remaining_permission"/>
- <perm_templates_users id="2" template_id="1" user_id="2" permission="existing_permission"/>
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission"/>
+ <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="existing_permission"/>
<perm_templates_groups/>
</dataset>
\ No newline at end of file
<dataset>
<permission_templates id="1" name="my template" description="my description" created_at="[null]" updated_at="[null]"/>
- <perm_templates_users id="1" template_id="1" user_id="1" permission="user_permission1"/>
- <perm_templates_users id="2" template_id="1" user_id="2" permission="user_permission1"/>
- <perm_templates_users id="3" template_id="1" user_id="2" permission="user_permission2"/>
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
+ <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
+ <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
<users id="1" login="user1" name="user1" />
<users id="2" login="user2" name="user2" />
- <perm_templates_groups id="1" template_id="1" group_id="1" permission="group_permission1"/>
- <perm_templates_groups id="2" template_id="1" group_id="2" permission="group_permission1"/>
- <perm_templates_groups id="3" template_id="1" group_id="[null]" permission="group_permission2"/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
+ <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
+ <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
<groups id="1" name="group1"/>
<groups id="2" name="group2"/>
create_table :perm_templates_users do |t|
t.column :user_id, :integer, :null => false
t.column :template_id, :integer, :null => false
- t.column :permission, :string, :null => false, :limit => 64
+ t.column :permission_reference, :string, :null => false, :limit => 64
t.column :created_at, :datetime, :null => true
t.column :updated_at, :datetime, :null => true
end
create_table :perm_templates_groups do |t|
t.column :group_id, :integer, :null => true
t.column :template_id, :integer, :null => false
- t.column :permission, :string, :null => false, :limit => 64
+ t.column :permission_reference, :string, :null => false, :limit => 64
t.column :created_at, :datetime, :null => true
t.column :updated_at, :datetime, :null => true
end