package io.smsc.security;

import io.smsc.model.Permission;
import io.smsc.model.Role;
import io.smsc.model.User;
import io.smsc.security.model.JWTUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Util class for creating {@link JWTUser} objects from {@link User} entities.
 *
 * @author  Nazar Lipkovskyy
 * @since   0.0.1-SNAPSHOT
 */
public final class JWTUserFactory {

    private JWTUserFactory() {
    }

    /**
     * Base method which creates {@link JWTUser} objects
     *
     * @param  user {@link User} entity which is user for {@link JWTUser} creating
     * @return      appropriate {@link JWTUser} object
     */
    public static JWTUser create(User user) {
        return new JWTUser(
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getSalt(),
                user.getFirstname(),
                user.getSurname(),
                user.getEmail(),
                user.isActive(),
                user.isBlocked(),
                user.getRoles(),
                mapToGrantedAuthorities(user.getRoles()));
    }

    /**
     * Base method which is used for creating set with {@link GrantedAuthority} from
     * set with {@link Role}
     *
     * @param  roles set with {@link Role} entities
     * @return       appropriate set with {@link GrantedAuthority} objects
     */
    private static Set<GrantedAuthority> mapToGrantedAuthorities(Set<Role> roles) {
        return getGrantedAuthorities(getPermissions(roles));
    }

    /**
     * Additional method which is used for creating set with strings which will describe
     * each {@link Permission} in each role from entry set.
     *
     * @param  roles set with {@link Role} entities
     * @return       appropriate set with string values of {@link Permission}
     */
    private static Set<String> getPermissions(Collection<Role> roles) {
        final Set<String> authorities = new HashSet<>();
        final List<Permission> collection = new ArrayList<>();
        for (final Role role : roles) {
            collection.addAll(role.getPermissions());
        }
        authorities.addAll(collection.stream().map(Permission::getName).collect(Collectors.toList()));
        return authorities;
    }

    /**
     * Additional method which is used for creating set with {@link GrantedAuthority} from set with
     * string values of {@link Permission}
     *
     * @param  permissions set with string values of {@link Permission}
     * @return             appropriate set with {@link GrantedAuthority} objects
     */
    private static Set<GrantedAuthority> getGrantedAuthorities(Set<String> permissions) {
        final Set<GrantedAuthority> authorities = new HashSet<>();
        for (final String authority : permissions) {
            authorities.add(new SimpleGrantedAuthority(authority));
        }
        return authorities;
    }
}

@@ -24,41 +24,8 @@
 y;%0A%0A
-import io.smsc.model.Permission;%0A
 impo
@@ -1583,703 +1583,163 @@
     
-return getGrantedAuthorities(getPermissions(roles));%0A    %7D%0A%0A    /**%0A     * Additio
+fi
 nal 
-method which is used for creating set with strings which will describe%0A     * each %7B@link Permission%7D in each role from entry set.%0A     *%0A     * @param  roles set with %7B@link Role%7D entities%0A     * @return       appropriate set with string values of %7B@link Permission%7D%0A     */%0A    private static Set%3CString%3E getPermissions(Collection%3CRole%3E roles) %7B%0A        final Set%3CString%3E authorities = new HashSet%3C%3E();%0A        final List%3CPermission%3E collection = new ArrayList%3C%3E();%0A        for (final Role role : roles) %7B%0A            collection.addAll(role.getPermissions());%0A        %7D%0A        authorities.addAll(collection
+Set%3CString%3E authorities = new HashSet%3C%3E();%0A        final Set%3CGrantedAuthority%3E grantedAuthorities = new HashSet%3C%3E();%0A        authorities.addAll(roles
 .str
@@ -1748,26 +1748,20 @@
 m().map(
-Permission
+Role
 ::getNam
@@ -1806,577 +1806,73 @@
     
-return authorities;%0A    %7D%0A%0A    /**%0A     * Additional method which is used for creating set with %7B@link GrantedAuthority%7D from set with%0A     * string values of %7B@link Permission%7D%0A     *%0A     * @param  permissions set with string values of %7B@link Permission%7D%0A     * @return             appropriate set with %7B@link GrantedAuthority%7D objects%0A     */%0A    private static Set%3CGrantedAuthority%3E getGrantedAuthorities(Set%3CString%3E permissions) %7B%0A        final Set%3CGrantedAuthority%3E authorities = new HashSet%3C%3E();%0A        for (final String authority : permissions) %7B%0A            a
+for (final String authority : authorities) %7B%0A            grantedA
 utho
@@ -1947,17 +1947,24 @@
  return 
-a
+grantedA
 uthoriti

Fix JWT model