Skip to content

Commit

Permalink
Fix name note permissions
Browse files Browse the repository at this point in the history
- rewrote section in svelte
- fixed lots of svelte warnings

#CTCTOWALTZ-2580
finos#6228
  • Loading branch information
db-waltz committed Oct 26, 2022
1 parent 88de8c1 commit 7faa6b6
Show file tree
Hide file tree
Showing 22 changed files with 829 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,49 @@

package org.finos.waltz.data.entity_named_note;

import org.finos.waltz.schema.tables.records.EntityNamedNoteTypeRecord;
import org.finos.waltz.common.Checks;
import org.finos.waltz.model.EntityKind;
import org.finos.waltz.model.*;
import org.finos.waltz.model.app_group.AppGroupMemberRole;
import org.finos.waltz.model.entity_named_note.EntityNamedNodeType;
import org.finos.waltz.model.entity_named_note.EntityNamedNoteTypeChangeCommand;
import org.finos.waltz.model.entity_named_note.ImmutableEntityNamedNodeType;
import org.finos.waltz.model.user.SystemRole;
import org.finos.waltz.schema.tables.ApplicationGroupMember;
import org.finos.waltz.schema.tables.EntityNamedNote;
import org.finos.waltz.schema.tables.EntityNamedNoteType;
import org.finos.waltz.schema.tables.UserRole;
import org.finos.waltz.schema.tables.records.EntityNamedNoteTypeRecord;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.finos.waltz.schema.tables.EntityNamedNote.ENTITY_NAMED_NOTE;
import static org.finos.waltz.schema.tables.EntityNamedNoteType.ENTITY_NAMED_NOTE_TYPE;
import static org.finos.waltz.common.Checks.checkNotNull;
import static org.finos.waltz.common.SetUtilities.asSet;
import static org.finos.waltz.common.SetUtilities.minus;
import static org.finos.waltz.common.StringUtilities.join;
import static org.finos.waltz.common.StringUtilities.splitThenMap;
import static org.finos.waltz.schema.tables.ApplicationGroupMember.APPLICATION_GROUP_MEMBER;
import static org.finos.waltz.schema.tables.EntityNamedNote.ENTITY_NAMED_NOTE;
import static org.finos.waltz.schema.tables.EntityNamedNoteType.ENTITY_NAMED_NOTE_TYPE;
import static org.finos.waltz.schema.tables.UserRole.USER_ROLE;

@Repository
public class EntityNamedNoteTypeDao {

private static final String SEPARATOR = ";";


private static final EntityNamedNoteType ennt = ENTITY_NAMED_NOTE_TYPE;
private static final RecordMapper<Record, EntityNamedNodeType> TO_DOMAIN_MAPPER = record -> {

EntityNamedNoteTypeRecord r = record.into(ENTITY_NAMED_NOTE_TYPE);
EntityNamedNoteTypeRecord r = record.into(ennt);

List<EntityKind> applicableEntityKinds = splitThenMap(
r.getApplicableEntityKinds(),
Expand All @@ -65,6 +78,9 @@ public class EntityNamedNoteTypeDao {
.externalId(Optional.ofNullable(r.getExternalId()))
.build();
};
private static final EntityNamedNote enn = ENTITY_NAMED_NOTE;
private static final UserRole ur = USER_ROLE;
private static final ApplicationGroupMember agm = APPLICATION_GROUP_MEMBER;


private final DSLContext dsl;
Expand All @@ -79,8 +95,8 @@ public EntityNamedNoteTypeDao(DSLContext dsl) {

public List<EntityNamedNodeType> findAll() {
return dsl
.select(ENTITY_NAMED_NOTE_TYPE.fields())
.from(ENTITY_NAMED_NOTE_TYPE)
.select(ennt.fields())
.from(ennt)
.fetch(TO_DOMAIN_MAPPER);
}

Expand All @@ -96,13 +112,13 @@ public List<EntityNamedNodeType> findAll() {
public boolean removeById(Long id) {

SelectConditionStep<Record1<Long>> anyUsageOfType = DSL
.select(ENTITY_NAMED_NOTE.ENTITY_ID)
.from(ENTITY_NAMED_NOTE)
.where(ENTITY_NAMED_NOTE.NAMED_NOTE_TYPE_ID.eq(id));
.select(enn.ENTITY_ID)
.from(enn)
.where(enn.NAMED_NOTE_TYPE_ID.eq(id));

return dsl
.deleteFrom(ENTITY_NAMED_NOTE_TYPE)
.where(ENTITY_NAMED_NOTE_TYPE.ID.eq(id))
.deleteFrom(ennt)
.where(ennt.ID.eq(id))
.andNotExists(anyUsageOfType)
.execute() == 1;
}
Expand All @@ -119,7 +135,7 @@ public long create(EntityNamedNoteTypeChangeCommand command) {
Set<EntityKind> applicableEntityKinds = Checks.checkOptionalIsPresent(command.applicableEntityKinds(), "Applicable Entity Kinds must be provided");
String kinds = join(applicableEntityKinds, SEPARATOR);

EntityNamedNoteTypeRecord record = dsl.newRecord(ENTITY_NAMED_NOTE_TYPE);
EntityNamedNoteTypeRecord record = dsl.newRecord(ennt);
record.setName(name);
record.setExternalId(command.externalId().orElse(""));
record.setDescription(command.description().orElse(""));
Expand All @@ -135,8 +151,8 @@ public long create(EntityNamedNoteTypeChangeCommand command) {

public boolean update(long id, EntityNamedNoteTypeChangeCommand command) {
EntityNamedNoteTypeRecord record = new EntityNamedNoteTypeRecord();
record.set(ENTITY_NAMED_NOTE_TYPE.ID, id);
record.changed(ENTITY_NAMED_NOTE_TYPE.ID, false);
record.set(ennt.ID, id);
record.changed(ennt.ID, false);

command.name()
.ifPresent(record::setName);
Expand All @@ -156,17 +172,88 @@ public boolean update(long id, EntityNamedNoteTypeChangeCommand command) {


public EntityNamedNodeType getById(long namedNoteTypeId) {
return dsl.select(ENTITY_NAMED_NOTE_TYPE.fields())
.from(ENTITY_NAMED_NOTE_TYPE)
.where(ENTITY_NAMED_NOTE_TYPE.ID.eq(namedNoteTypeId))
return dsl.select(ennt.fields())
.from(ennt)
.where(ennt.ID.eq(namedNoteTypeId))
.fetchOne(TO_DOMAIN_MAPPER);
}

public EntityNamedNodeType getByExternalId(String externalId) {
return dsl
.select(ENTITY_NAMED_NOTE_TYPE.fields())
.from(ENTITY_NAMED_NOTE_TYPE)
.where(ENTITY_NAMED_NOTE_TYPE.EXTERNAL_ID.eq(externalId))
.select(ennt.fields())
.from(ennt)
.where(ennt.EXTERNAL_ID.eq(externalId))
.fetchOne(TO_DOMAIN_MAPPER);
}


public Set<EntityWithOperations<EntityNamedNodeType>> findForRefAndUser(EntityReference ref,
String username) {

SystemRole requiredRole = SystemRole.APP_EDITOR;

SelectConditionStep<Record> qry = dsl
.select(ennt.fields())
.select(enn.LAST_UPDATED_AT) // already has a note ?
.select(ur.USER_NAME) // user has role ?
.select(agm.USER_ID) // user is part of app group
.from(ennt)
.leftJoin(enn)
.on(enn.NAMED_NOTE_TYPE_ID.eq(ennt.ID)
.and(enn.ENTITY_ID.eq(ref.id()))
.and(enn.ENTITY_KIND.eq(ref.kind().name())))
.leftJoin(ur)
.on(ur.USER_NAME.eq(username)
.and(ur.ROLE.eq(requiredRole.name())))
.leftJoin(agm)
.on(ref.kind() == EntityKind.APP_GROUP
? DSL.trueCondition()
: DSL.falseCondition())
.and(agm.GROUP_ID.eq(ref.id()))
.and(agm.USER_ID.eq(username))
.and(agm.ROLE.eq(AppGroupMemberRole.OWNER.name()))
.where(ennt.APPLICABLE_ENTITY_KINDS.like(String.format("%%%s%%", ref.kind())));

return qry
.fetch()
.stream()
.map(r -> {
EntityNamedNodeType entityNamedNodeType = TO_DOMAIN_MAPPER.map(r);
boolean alreadyExists = r.get(enn.LAST_UPDATED_AT) != null;
boolean hasBasicRole = r.get(ur.USER_NAME) != null;
boolean isAppGroup = ref.kind() == EntityKind.APP_GROUP;
boolean hasAppGroupOwnership = r.get(agm.USER_ID) != null;

Set<Operation> ops = asSet(
Operation.ADD,
Operation.UPDATE,
Operation.REMOVE);

if (entityNamedNodeType.isReadOnly()) {
ops = new HashSet<>();
}

if (alreadyExists) {
ops = minus(ops, asSet(Operation.ADD));
} else {
ops = minus(ops, asSet(Operation.UPDATE, Operation.REMOVE));
}

if (isAppGroup && ! hasAppGroupOwnership) {
ops = new HashSet<>();
}

if (!isAppGroup && !hasBasicRole) {
ops = new HashSet<>();
}

return ImmutableEntityWithOperations
.<EntityNamedNodeType>builder()
.entity(entityNamedNodeType)
.addAllOperations(ops)
.build();
})
.collect(Collectors.toSet());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Waltz - Enterprise Architecture
* Copyright (C) 2016, 2017, 2018, 2019 Waltz open source project
* See README.md for more information
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific
*
*/

package org.finos.waltz.jobs.harness;


import org.finos.waltz.data.entity_named_note.EntityNamedNoteTypeDao;
import org.finos.waltz.model.EntityKind;
import org.finos.waltz.service.DIConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.finos.waltz.model.EntityReference.mkRef;

public class EntityNamedNoteTypeHarness {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
EntityNamedNoteTypeDao dao = ctx.getBean(EntityNamedNoteTypeDao.class);

//
// System.out.println("\n\nTitch");
// System.out.println(dao.findForRefAndUser(
// mkRef(EntityKind.APP_GROUP, 27342),
// "david.watkins@db.com"));
//
// System.out.println("\n\nTitch Derived");
// System.out.println(dao.findForRefAndUser(
// mkRef(EntityKind.APP_GROUP, 27343),
// "david.watkins@db.com"));

System.out.println("\n\nAuto");
System.out.println(dao.findForRefAndUser(
mkRef(EntityKind.APP_GROUP, 491),
"david.watkins@db.com"));
//
// System.out.println("\n\nMTNA Me");
// System.out.println(dao.findForRefAndUser(
// mkRef(EntityKind.APPLICATION, 19399),
// "david.watkins@db.com"));
//
// System.out.println("\n\nMTNA Nobody");
// System.out.println(dao.findForRefAndUser(
// mkRef(EntityKind.APPLICATION, 19399),
// "nobody.special@db.com"));


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.finos.waltz.model;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;

import java.util.Set;

@Value.Immutable
@JsonSerialize(as = ImmutableEntityWithOperations.class)
public abstract class EntityWithOperations<T> {

public abstract T entity();
public abstract Set<Operation> operations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
text-align: right
}
.summary-table .derived {
font-style: italic;
}
svg {
display: block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
text-align: right
}
.summary-table .derived {
font-style: italic;
}
svg {
display: block;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@
</li>
{:else}
<li class="force-wrap">
<a class="clickable"
on:click={() => selectAssessment(row)}>
<button class="btn-link"
on:click={() => selectAssessment(row)}>
{row.definition.name}
</a>
</button>
</li>
{/if}
{/each}
Expand Down
Loading

0 comments on commit 7faa6b6

Please sign in to comment.