/*
 * Copyright 2017 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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 language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.identity.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Stores user credentials, which includes the user's name, the password and the user's roles.
 * @since 5.11.0
 */
public final class User implements Serializable {

	public static final User SYSTEM = new User("System", Collections.singletonList(Role.ADMINISTRATOR));
	
	private final String username;
	private final List<Role> roles;

	public User(String username, List<Role> roles) {
		this.username = username;
		this.roles = roles;
	}
	
	public String getUsername() {
		return username;
	}
	
	public List<Role> getRoles() {
		return roles;
	}
	
	@Override
	public int hashCode() {
		return Objects.hash(username);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		User other = (User) obj;
		return Objects.equals(username, other.username);
	}

	public boolean isAdministrator() {
		return getRoles().contains(Role.ADMINISTRATOR);
	}
	
	public boolean hasPermission(String permission) {
		return getRoles().stream()
			.map(Role::getPermissions)
			.flatMap(Collection::stream)
			.collect(Collectors.toSet())
			.contains(permission);
	}

	public static boolean isSystem(String userId) {
		return SYSTEM.getUsername().equals(userId);
	}
	
}
@@ -844,16 +844,69 @@
 ctors;%0A%0A
+import com.fasterxml.jackson.annotation.JsonIgnore;%0A%0A
 /**%0A * S
@@ -1447,24 +1447,204 @@
 roles;%0A%09%7D%0A%09%0A
+%09@JsonIgnore%0A%09public List%3CPermission%3E getPermissions() %7B%0A%09%09return getRoles().stream().flatMap(role -%3E role.getPermissions().stream()).distinct().collect(Collectors.toList());%0A%09%7D%0A%09%0A
 %09@Override%0A%09
@@ -2343,11 +2343,10 @@
 Id);%0A%09%7D%0A
-%09
 %0A%7D

add getPermissions to User domain class