freactoring to get company list work in efficient way
This commit is contained in:
@@ -19,9 +19,7 @@ import de.srsoftware.umbrella.core.model.Company;
|
||||
import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
|
||||
@@ -58,8 +56,9 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
}
|
||||
|
||||
private boolean getCompanyList(UmbrellaUser user, HttpExchange ex) throws IOException, UmbrellaException {
|
||||
var list = listCompaniesOf(user).stream().map(Company::toMap);
|
||||
return sendContent(ex,list);
|
||||
var result = new HashMap<Long,Map<String,Object>>();
|
||||
for (var entry : listCompaniesOf(user).entrySet()) result.put(entry.getKey(),entry.getValue().toMap());
|
||||
return sendContent(ex,result);
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +71,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Company> listCompaniesOf(UmbrellaUser user) throws UmbrellaException {
|
||||
public Map<Long,Company> listCompaniesOf(UmbrellaUser user) throws UmbrellaException {
|
||||
return companyDb.listCompaniesOf(user.id());
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,9 @@ import de.srsoftware.umbrella.core.model.Company;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
public class SqliteDb implements CompanyDb {
|
||||
|
||||
@@ -38,11 +40,14 @@ public class SqliteDb implements CompanyDb {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Company> listCompaniesOf(long userId) throws UmbrellaException {
|
||||
public Map<Long,Company> listCompaniesOf(long userId) throws UmbrellaException {
|
||||
try {
|
||||
var rs = select("*").from(TABLE_COMPANIES).leftJoin(ID,TABLE_COMPANIES_USERS,COMPANY_ID).where(USER_ID,equal(userId)).exec(db);
|
||||
var companies = new HashSet<Company>();
|
||||
while (rs.next()) companies.add(Company.of(rs));
|
||||
var companies = new HashMap<Long,Company>();
|
||||
while (rs.next()) {
|
||||
var company = Company.of(rs);
|
||||
companies.put(company.id(),company);
|
||||
}
|
||||
rs.close();
|
||||
return companies;
|
||||
} catch (SQLException e) {
|
||||
|
||||
@@ -4,11 +4,12 @@ package de.srsoftware.umbrella.company.api;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface CompanyDb {
|
||||
Collection<Long> getMembers(long companyId) throws UmbrellaException;
|
||||
|
||||
Collection<Company> listCompaniesOf(long id) throws UmbrellaException;
|
||||
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
|
||||
|
||||
Company load(long companyId) throws UmbrellaException;
|
||||
}
|
||||
|
||||
@@ -52,12 +52,14 @@ public class Constants {
|
||||
public static final String PARENT_TASK_ID = "parent_task_id";
|
||||
public static final String PASS = "pass";
|
||||
public static final String PASSWORD = "password";
|
||||
public static final String PERMISSION = "permission";
|
||||
public static final String POST = "POST";
|
||||
public static final String PROJECT_ID = "project_id";
|
||||
|
||||
public static final String RECEIVERS = "receivers";
|
||||
public static final String REDIRECT = "redirect";
|
||||
public static final String RENDERED = "rendered";
|
||||
|
||||
public static final String SENDER = "sender";
|
||||
public static final String SETTINGS = "settings";
|
||||
public static final String SHOW_CLOSED = "show_closed";
|
||||
@@ -69,13 +71,14 @@ public class Constants {
|
||||
public static final String STATUS_CODE = "code";
|
||||
public static final String STRING = "string";
|
||||
public static final String SUBJECT = "subject";
|
||||
public static final String TABLE_SETTINGS = "settings";
|
||||
|
||||
public static final String TABLE_SETTINGS = "settings";
|
||||
public static final String TEMPLATE = "template";
|
||||
public static final String TEXT = "text";
|
||||
public static final String THEME = "theme";
|
||||
public static final String TITLE = "title";
|
||||
public static final String TOKEN = "token";
|
||||
|
||||
public static final String UMBRELLA = "Umbrella";
|
||||
public static final String URL = "url";
|
||||
public static final String USER = "user";
|
||||
|
||||
@@ -5,13 +5,14 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface CompanyService {
|
||||
Company get(long companyId) throws UmbrellaException;
|
||||
|
||||
Collection<UmbrellaUser> getMembers(long companyId) throws UmbrellaException;
|
||||
|
||||
Collection<Company> listCompaniesOf(UmbrellaUser user) throws UmbrellaException;
|
||||
Map<Long,Company> listCompaniesOf(UmbrellaUser user) throws UmbrellaException;
|
||||
|
||||
boolean membership(long companyId, long userId) throws UmbrellaException;
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@ package de.srsoftware.umbrella.core.api;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Project;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface ProjectService {
|
||||
public Collection<Project> listProjectsOfCompany(long companyId, boolean includeClosed) throws UmbrellaException;
|
||||
|
||||
public Collection<Project> listCompanyProjects(long companyId, boolean includeClosed) throws UmbrellaException;
|
||||
public Map<Long,Project> listUserProjects(long userId, boolean includeClosed) throws UmbrellaException;
|
||||
CompanyService companyService();
|
||||
}
|
||||
|
||||
@@ -2,4 +2,16 @@
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
|
||||
public record Member(UmbrellaUser user, Permission permission){ }
|
||||
import de.srsoftware.tools.Mappable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.PERMISSION;
|
||||
import static de.srsoftware.umbrella.core.Constants.USER;
|
||||
|
||||
public record Member(UmbrellaUser user, Permission permission) implements Mappable {
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(USER,user.toMap(),PERMISSION,permission.name());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public record Project(long id, String name, String description, Status status, L
|
||||
map.put(STATUS,Map.of(STATUS_CODE,status.code(), NAME,status.name()));
|
||||
map.put(COMPANY_ID,companyId);
|
||||
map.put(SHOW_CLOSED,showClosed);
|
||||
map.put(MEMBERS,members == null ? List.of() : members);
|
||||
map.put(MEMBERS,members == null ? List.of() : members.stream().map(Member::toMap).toList());
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,6 @@ public class DocumentApi extends BaseHandler {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head){
|
||||
case COMPANIES -> getCompanies(ex,user.get(),token.orElse(null));
|
||||
case CONTACTS -> getContacts(ex,user.get(),token.orElse(null));
|
||||
case PATH_TYPES -> getDocTypes(ex);
|
||||
case STATES -> getDocStates(ex);
|
||||
@@ -232,10 +231,6 @@ public class DocumentApi extends BaseHandler {
|
||||
return ok(ex);
|
||||
}
|
||||
|
||||
private boolean getCompanies(HttpExchange ex, UmbrellaUser user, Token token) throws IOException, UmbrellaException {
|
||||
return sendContent(ex,companies.listCompaniesOf(user).stream().map(Company::toMap));
|
||||
}
|
||||
|
||||
private boolean getContacts(HttpExchange ex, UmbrellaUser user, Token token) throws IOException, UmbrellaException {
|
||||
return sendContent(ex,getLegacyContacts(ex,user,token));
|
||||
}
|
||||
|
||||
@@ -20,13 +20,11 @@
|
||||
});
|
||||
|
||||
async function loadCompanies(){
|
||||
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/document/companies`;
|
||||
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/company/list`;
|
||||
var resp = await fetch(url,{ credentials: 'include'});
|
||||
if (resp.ok){
|
||||
const companies = await resp.json();
|
||||
for (let c of companies) {
|
||||
if (c.id == document.sender.company) company = c;
|
||||
}
|
||||
company = companies[document.sender.company];
|
||||
document.sender.name = '';
|
||||
if (company.name) document.sender.name += company.name+"\n";
|
||||
if (company.address) document.sender.name += company.address+"\n";
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
let docType = 0;
|
||||
|
||||
async function loadCompanies(){
|
||||
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/document/companies`;
|
||||
const url = `${location.protocol}//${location.host.replace('5173','8080')}/api/company/list`;
|
||||
var resp = await fetch(url,{ credentials: 'include'});
|
||||
if (resp.ok){
|
||||
companies = await resp.json();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<script>
|
||||
import { useTinyRouter } from 'svelte-tiny-router';
|
||||
import { onMount } from 'svelte';
|
||||
import { t } from '../../translations.svelte.js';
|
||||
|
||||
import CompanySelector from '../../Components/CompanySelector.svelte';
|
||||
|
||||
@@ -4,11 +4,69 @@
|
||||
import { t } from '../../translations.svelte.js';
|
||||
|
||||
const router = useTinyRouter();
|
||||
let error = $state(null);
|
||||
let projects = $state(null);
|
||||
let companies = $state(null);
|
||||
|
||||
async function loadProjects(){
|
||||
let url = `${location.protocol}//${location.host.replace('5173','8080')}/api/company/list`;
|
||||
let resp = await fetch(url,{credentials:'include'});
|
||||
if (resp.ok){
|
||||
companies = await resp.json();
|
||||
url = `${location.protocol}//${location.host.replace('5173','8080')}/api/project/list`;
|
||||
resp = await fetch(url,{credentials:'include'});
|
||||
if (resp.ok){
|
||||
projects = await resp.json();
|
||||
} else {
|
||||
error = await resp.text();
|
||||
}
|
||||
} else {
|
||||
error = await resp.text();
|
||||
}
|
||||
}
|
||||
|
||||
onMount(loadProjects);
|
||||
</script>
|
||||
|
||||
{#if error}
|
||||
<span class="error">{error}</span>
|
||||
{/if}
|
||||
<fieldset>
|
||||
<legend>
|
||||
{t('projects')}
|
||||
<button onclick={() => router.navigate('/project/add')}>{t('create_new')}</button>
|
||||
</legend>
|
||||
{#if projects}
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{t('name')}</th>
|
||||
<th>{t('company')}</th>
|
||||
<th>{t('state')}</th>
|
||||
<th>{t('members')}</th>
|
||||
<th>{t('actions')}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#each Object.entries(projects) as [id,project]}
|
||||
<tr onclick={() => router.navigate(`/project/${project.id}/view`)}>
|
||||
<td>{project.name}</td>
|
||||
<td>
|
||||
{#if project.company_id}
|
||||
{companies[project.company_id].name}
|
||||
{/if}
|
||||
</td>
|
||||
<td>
|
||||
{t("state_"+project.status.name.toLowerCase())}
|
||||
</td>
|
||||
<td>
|
||||
{#each project.members as member,idx}
|
||||
<div>{member.user.name}</div>
|
||||
{/each}
|
||||
</td>
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
{/if}
|
||||
</fieldset>
|
||||
@@ -7,7 +7,7 @@ public class Constants {
|
||||
public static final String DB_VERSION = "project_db_version";
|
||||
public static final String PERMISSIONS = "permissions";
|
||||
public static final String TABLE_PROJECTS = "projects";
|
||||
public static final String TABLE_PROJECT_USERS = "project_users";
|
||||
public static final String TABLE_PROJECT_USERS = "projects_users";
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ import de.srsoftware.umbrella.core.model.Project;
|
||||
import java.util.Map;
|
||||
|
||||
public interface ProjectDb {
|
||||
Map<Long, Project> list(long companyId, boolean includeClosed, UserService userService) throws UmbrellaException;
|
||||
Map<Long, Project> ofCompany(long companyId, boolean includeClosed, UserService userService) throws UmbrellaException;
|
||||
Map<Long, Project> ofUser(long userId, boolean includeClosed, UserService userService) throws UmbrellaException;
|
||||
|
||||
Project save(Project prj) throws UmbrellaException;
|
||||
}
|
||||
|
||||
@@ -26,13 +26,13 @@ import org.json.JSONObject;
|
||||
|
||||
public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
|
||||
private final ProjectDb projectDb;
|
||||
private final ProjectDb projects;
|
||||
private final CompanyService companies;
|
||||
private final UserService users;
|
||||
|
||||
public ProjectModule(Configuration config, CompanyService companyService) throws UmbrellaException {
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
projectDb = new SqliteDb(connect(dbFile));
|
||||
projects = new SqliteDb(connect(dbFile));
|
||||
companies = companyService;
|
||||
users = companies.userService();
|
||||
}
|
||||
@@ -42,6 +42,24 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
return companies;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doGet(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = users.loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LIST -> listUserProjects(ex,user.get());
|
||||
case null -> postProject(ex,user.get());
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
@@ -51,7 +69,7 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LIST -> listProjects(ex,user.get());
|
||||
case LIST -> listCompanyProjects(ex,user.get());
|
||||
case null -> postProject(ex,user.get());
|
||||
default -> super.doGet(path,ex);
|
||||
};
|
||||
@@ -60,24 +78,35 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean listProjects(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
|
||||
public Collection<Project> listCompanyProjects(long companyId, boolean includeClosed) throws UmbrellaException {
|
||||
return projects.ofCompany(companyId, includeClosed, users).values().stream().sorted(comparing(Project::name)).toList();
|
||||
}
|
||||
|
||||
private boolean listCompanyProjects(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(COMPANY_ID) && json.get(COMPANY_ID) instanceof Number cid)) throw missingFieldException(COMPANY_ID);
|
||||
var companyId = cid.longValue();
|
||||
var company = companies.get(companyId);
|
||||
if (!companies.membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
var projects = listProjectsOfCompany(companyId,false)
|
||||
var projects = listCompanyProjects(companyId,false)
|
||||
.stream()
|
||||
.map(Project::toMap)
|
||||
.map(HashMap::new);
|
||||
return sendContent(ex,projects);
|
||||
}
|
||||
|
||||
public Collection<Project> listProjectsOfCompany(long companyId, boolean includeClosed) throws UmbrellaException {
|
||||
|
||||
return projectDb.list(companyId, includeClosed, users).values().stream().sorted(comparing(Project::name)).toList();
|
||||
@Override
|
||||
public Map<Long, Project> listUserProjects(long userId, boolean includeClosed) throws UmbrellaException {
|
||||
return projects.ofUser(userId, includeClosed, users);
|
||||
}
|
||||
|
||||
private boolean listUserProjects(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
|
||||
var projects = new HashMap<Long,Map<String,Object>>();
|
||||
for (var entry : listUserProjects(user.id(),false).entrySet()) projects.put(entry.getKey(),entry.getValue().toMap());
|
||||
return sendContent(ex,projects);
|
||||
}
|
||||
|
||||
|
||||
private boolean postProject(HttpExchange ex, UmbrellaUser user) throws IOException, UmbrellaException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(NAME) && json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
|
||||
@@ -92,7 +121,7 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
||||
showClosed = settingsJson.has(SHOW_CLOSED) && settingsJson.get(SHOW_CLOSED) == TRUE;
|
||||
}
|
||||
var prj = new Project(0,name,description,Project.Status.Open,companyId,showClosed, List.of(new Member(user, OWNER)));
|
||||
prj = projectDb.save(prj);
|
||||
prj = projects.save(prj);
|
||||
return sendContent(ex,prj);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import static de.srsoftware.tools.jdbc.Query.select;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.TABLE_SETTINGS;
|
||||
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_SERVER_ERROR;
|
||||
import static de.srsoftware.umbrella.core.Util.LOG;
|
||||
import static de.srsoftware.umbrella.core.model.Project.Status.Open;
|
||||
import static de.srsoftware.umbrella.project.Constants.*;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
@@ -36,6 +35,31 @@ public class SqliteDb implements ProjectDb {
|
||||
init();
|
||||
}
|
||||
|
||||
private HashMap<Long, Project> addMembers(HashMap<Long, Project> projects, UserService userService) throws SQLException, UmbrellaException {
|
||||
Object[] ids = projects.keySet().toArray();
|
||||
var rs = select("*").from(TABLE_PROJECT_USERS).where(PROJECT_ID,in(ids)).exec(db);
|
||||
var userIdMap = new HashMap<Long,HashMap<Long, Permission>>();
|
||||
while (rs.next()){
|
||||
var userId = rs.getLong(USER_ID);
|
||||
var projectId = rs.getLong(PROJECT_ID);
|
||||
var permission = Permission.of(rs.getInt(PERMISSIONS));
|
||||
HashMap<Long, Permission> userMap = userIdMap.computeIfAbsent(userId, k -> new HashMap<>());
|
||||
userMap.put(projectId,permission);
|
||||
}
|
||||
rs.close();
|
||||
var userMap = userService.list(userIdMap.keySet());
|
||||
for (var entry : userIdMap.entrySet()){
|
||||
var userId = entry.getKey();
|
||||
var user = userMap.get(userId);
|
||||
for (var inner : entry.getValue().entrySet()){
|
||||
var projectId = inner.getKey();
|
||||
var perm = inner.getValue();
|
||||
var project = projects.get(projectId);
|
||||
project.members().add(new Member(user,perm));
|
||||
}
|
||||
}
|
||||
return projects;
|
||||
}
|
||||
|
||||
private int createTables() {
|
||||
createProjectTables();
|
||||
@@ -123,7 +147,7 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashMap<Long, Project> list(long companyId, boolean includeClosed, UserService userService) throws UmbrellaException {
|
||||
public HashMap<Long, Project> ofCompany(long companyId, boolean includeClosed, UserService userService) throws UmbrellaException {
|
||||
try {
|
||||
var projects = new HashMap<Long,Project>();
|
||||
var query = select("*").from(TABLE_PROJECTS).where(COMPANY_ID, equal(companyId));
|
||||
@@ -134,28 +158,27 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
||||
projects.put(project.id(),project);
|
||||
}
|
||||
rs.close();
|
||||
rs = select("*").from(TABLE_PROJECT_USERS).where(PROJECT_ID,in(projects.keySet())).exec(db);
|
||||
var userIdMap = new HashMap<Long,HashMap<Long, Permission>>();
|
||||
return addMembers(projects,userService);
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException(HTTP_SERVER_ERROR,"Failed to load items from database");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public HashMap<Long, Project> ofUser(long userId, boolean includeClosed, UserService userService) throws UmbrellaException {
|
||||
try {
|
||||
var projects = new HashMap<Long,Project>();
|
||||
var query = select("*").from(TABLE_PROJECTS).leftJoin(ID,TABLE_PROJECT_USERS,PROJECT_ID).where(USER_ID, equal(userId));
|
||||
if (!includeClosed) query = query.where(STATUS,lessThan(Project.Status.Complete.code()));
|
||||
var rs = query.exec(db);
|
||||
while (rs.next()){
|
||||
var userId = rs.getLong(USER_ID);
|
||||
var projectId = rs.getLong(PROJECT_ID);
|
||||
var permission = Permission.of(rs.getInt(PERMISSIONS));
|
||||
HashMap<Long, Permission> userMap = userIdMap.computeIfAbsent(userId, k -> new HashMap<>());
|
||||
userMap.put(projectId,permission);
|
||||
var project = Project.of(rs);
|
||||
projects.put(project.id(),project);
|
||||
}
|
||||
rs.close();
|
||||
var userMap = userService.list(userIdMap.keySet());
|
||||
for (var entry : userIdMap.entrySet()){
|
||||
var userId = entry.getKey();
|
||||
var user = userMap.get(userId);
|
||||
for (var inner : entry.getValue().entrySet()){
|
||||
var projectId = inner.getKey();
|
||||
var perm = inner.getValue();
|
||||
var project = projects.get(projectId);
|
||||
project.members().add(new Member(user,perm));
|
||||
}
|
||||
}
|
||||
return projects;
|
||||
return addMembers(projects,userService);
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException(HTTP_SERVER_ERROR,"Failed to load items from database");
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class TaskModule extends BaseHandler implements TaskService {
|
||||
var companyId = cid.longValue();
|
||||
var company = companies.get(companyId);
|
||||
if (!companies.membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
var projects = this.projects.listProjectsOfCompany(companyId,false);
|
||||
var projects = this.projects.listCompanyProjects(companyId,false);
|
||||
var taskList = taskDb.listTasks(projects.stream().map(Project::id).toList());
|
||||
var map = taskList.stream().collect(toMap(Task::id, t -> t));
|
||||
var tree = new HashMap<Long,Map<String,Object>>();
|
||||
@@ -89,7 +89,7 @@ public class TaskModule extends BaseHandler implements TaskService {
|
||||
|
||||
@Override
|
||||
public Collection<Task> listCompanyTasks(long companyId) throws UmbrellaException {
|
||||
var projectList = projects.listProjectsOfCompany(companyId,false);
|
||||
var projectList = projects.listCompanyProjects(companyId,false);
|
||||
return taskDb.listTasks(projectList.stream().map(Project::id).toList());
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@
|
||||
"logout": "Abmelden",
|
||||
|
||||
"MANAGE_LOGIN_SERVICES": "Login-Services verwalten",
|
||||
"members": "Mitarbeiter",
|
||||
"message": "Nachricht",
|
||||
"messages": "Benachrichtigungen",
|
||||
"model": "Modelle",
|
||||
@@ -141,6 +142,7 @@
|
||||
"state_delayed": "verspätet",
|
||||
"state_error": "Fehler",
|
||||
"state_new":"neu",
|
||||
"state_open": "offen",
|
||||
"state_payed": "bezahlt",
|
||||
"state_sent": "versendet",
|
||||
"status" : {
|
||||
|
||||
@@ -16,8 +16,8 @@ import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.user.Constants.*;
|
||||
import static de.srsoftware.umbrella.user.Paths.*;
|
||||
import static de.srsoftware.umbrella.user.Paths.IMPERSONATE;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.*;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.Permission.*;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.Permission;
|
||||
import static java.lang.System.Logger.Level.*;
|
||||
import static java.net.HttpURLConnection.*;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
@@ -396,7 +396,7 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
private boolean impersonate(HttpExchange ex, Long targetId) throws IOException, UmbrellaException {
|
||||
var requestingUser = loadUser(ex);
|
||||
if (!(requestingUser.isPresent() && requestingUser.get() instanceof DbUser dbUser)) return unauthorized(ex);
|
||||
if (!dbUser.permissions().contains(PERMISSION.IMPERSONATE)) throw forbidden("You are not allowed to impersonate other users!");
|
||||
if (!dbUser.permissions().contains(Permission.IMPERSONATE)) throw forbidden("You are not allowed to impersonate other users!");
|
||||
if (targetId == null) return sendContent(ex,HTTP_UNPROCESSABLE,"user id missing");
|
||||
var targetUser = loadUser(targetId);
|
||||
users.getSession(targetUser).cookie().addTo(ex);
|
||||
@@ -456,7 +456,7 @@ public class UserModule extends BaseHandler implements UserService {
|
||||
private boolean postCreate(HttpExchange ex) throws IOException, UmbrellaException {
|
||||
var optUser = loadUser(ex);
|
||||
if (!(optUser.isPresent() && optUser.get() instanceof DbUser dbUser)) return unauthorized(ex);
|
||||
if (!dbUser.permissions().contains(PERMISSION.CREATE_USERS)) throw forbidden("You are not allowed to create new users!");
|
||||
if (!dbUser.permissions().contains(Permission.CREATE_USERS)) throw forbidden("You are not allowed to create new users!");
|
||||
var json = json(ex);
|
||||
|
||||
if (json.has(USER)) json = json.getJSONObject(USER);
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
package de.srsoftware.umbrella.user.model;
|
||||
|
||||
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.*;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.IMPERSONATE;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.LIST_USERS;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.MANAGE_LOGIN_SERVICES;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.Permission.*;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.Permission.IMPERSONATE;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.Permission.LIST_USERS;
|
||||
import static de.srsoftware.umbrella.user.model.DbUser.Permission.MANAGE_LOGIN_SERVICES;
|
||||
|
||||
import de.srsoftware.umbrella.core.model.EmailAddress;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
@@ -14,7 +14,7 @@ import java.util.Set;
|
||||
|
||||
public class DbUser extends UmbrellaUser {
|
||||
|
||||
public enum PERMISSION {
|
||||
public enum Permission {
|
||||
CREATE_USERS,
|
||||
UPDATE_USERS,
|
||||
DELETE_USERS,
|
||||
@@ -23,13 +23,13 @@ public class DbUser extends UmbrellaUser {
|
||||
MANAGE_LOGIN_SERVICES
|
||||
}
|
||||
|
||||
public static Set<PERMISSION> ADMIN_PERMISSIONS = Set.of(CREATE_USERS, UPDATE_USERS, DELETE_USERS, IMPERSONATE, MANAGE_LOGIN_SERVICES,LIST_USERS);
|
||||
public static Set<Permission> ADMIN_PERMISSIONS = Set.of(CREATE_USERS, UPDATE_USERS, DELETE_USERS, IMPERSONATE, MANAGE_LOGIN_SERVICES,LIST_USERS);
|
||||
|
||||
private final Set<PERMISSION> permissions;
|
||||
private final Set<Permission> permissions;
|
||||
private final Password hashedPass;
|
||||
private final Long lastLogoff;
|
||||
|
||||
public DbUser(long id, String name, EmailAddress email, Password hashedPassword, String theme, String languageCode, Set<PERMISSION> permissions, Long lastLogoff) {
|
||||
public DbUser(long id, String name, EmailAddress email, Password hashedPassword, String theme, String languageCode, Set<Permission> permissions, Long lastLogoff) {
|
||||
super(id, name, email, theme, languageCode);
|
||||
this.hashedPass = hashedPassword;
|
||||
this.permissions = permissions;
|
||||
@@ -44,11 +44,11 @@ public class DbUser extends UmbrellaUser {
|
||||
return lastLogoff;
|
||||
}
|
||||
|
||||
public boolean may(PERMISSION permission){
|
||||
public boolean may(Permission permission){
|
||||
return permissions.contains(permission);
|
||||
}
|
||||
|
||||
public Set<PERMISSION> permissions() {
|
||||
public Set<Permission> permissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
|
||||
@@ -524,7 +524,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
|
||||
private DbUser toUser(ResultSet rs) throws SQLException, UmbrellaException {
|
||||
long id = rs.getLong(ID);
|
||||
Set<DbUser.PERMISSION> perms = id == 1 ? ADMIN_PERMISSIONS : Set.of();
|
||||
Set<DbUser.Permission> perms = id == 1 ? ADMIN_PERMISSIONS : Set.of();
|
||||
return new DbUser(
|
||||
id,
|
||||
rs.getString(LOGIN),
|
||||
|
||||
Reference in New Issue
Block a user