Project

General

Profile

1
package eu.dnetlib.organizations.controller;
2

    
3
import org.springframework.security.core.Authentication;
4
import org.springframework.security.core.GrantedAuthority;
5

    
6
import eu.dnetlib.organizations.utils.OpenOrgsConstants;
7

    
8
public class UserInfo {
9

    
10
	private String name;
11
	private boolean superUser;
12

    
13
	public UserInfo() {
14
		this.name = "anonymous";
15
		this.superUser = false;
16
	}
17

    
18
	public UserInfo(final String name, final boolean superUser) {
19
		this.name = name;
20
		this.superUser = superUser;
21
	}
22

    
23
	public String getName() {
24
		return name;
25
	}
26

    
27
	public void setName(final String name) {
28
		this.name = name;
29
	}
30

    
31
	public boolean isSuperUser() {
32
		return superUser;
33
	}
34

    
35
	public void setSuperUser(final boolean superUser) {
36
		this.superUser = superUser;
37
	}
38

    
39
	public static UserInfo generate(final Authentication authentication) {
40
		return new UserInfo(authentication.getName(), isSuperUser(authentication));
41
	}
42

    
43
	public static boolean isSuperUser(final Authentication authentication) {
44
		for (final GrantedAuthority aut : authentication.getAuthorities()) {
45
			if (aut.getAuthority().equals("ROLE_" + OpenOrgsConstants.superUserRole)) { return true; }
46
		}
47
		return false;
48
	}
49

    
50
	public static boolean isSimpleUser(final Authentication authentication) {
51
		for (final GrantedAuthority aut : authentication.getAuthorities()) {
52
			if (aut.getAuthority().equals("ROLE_" + OpenOrgsConstants.userRole)) { return true; }
53
		}
54
		return false;
55
	}
56

    
57
	public static boolean isPending(final Authentication authentication) {
58
		for (final GrantedAuthority aut : authentication.getAuthorities()) {
59
			if (aut.getAuthority().equals("ROLE_" + OpenOrgsConstants.pendingRole)) { return true; }
60
		}
61
		return false;
62
	}
63

    
64
	public static boolean isNotAuthorized(final Authentication authentication) {
65
		for (final GrantedAuthority aut : authentication.getAuthorities()) {
66
			if (aut.getAuthority().equals("ROLE_" + OpenOrgsConstants.notAuthorizedRole)) { return true; }
67
		}
68
		return false;
69
	}
70

    
71
}
(4-4/5)