View Javadoc
1   package org.argeo.cms.integration;
2   
3   import java.io.Serializable;
4   import java.util.Arrays;
5   import java.util.Collections;
6   import java.util.Set;
7   import java.util.TreeSet;
8   
9   import org.argeo.cms.auth.CmsSession;
10  import org.osgi.service.useradmin.Authorization;
11  
12  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
13  
14  /** A serializable descriptor of an internal {@link CmsSession}. */
15  @JsonIgnoreProperties(ignoreUnknown = true)
16  public class CmsSessionDescriptor implements Serializable, Authorization {
17  	private static final long serialVersionUID = 8592162323372641462L;
18  
19  	private String name;
20  	private String cmsSessionId;
21  	private String displayName;
22  	private String locale;
23  	private Set<String> roles;
24  
25  	public CmsSessionDescriptor() {
26  	}
27  
28  	public CmsSessionDescriptor(String name, String cmsSessionId, String[] roles, String displayName, String locale) {
29  		this.name = name;
30  		this.displayName = displayName;
31  		this.cmsSessionId = cmsSessionId;
32  		this.locale = locale;
33  		this.roles = Collections.unmodifiableSortedSet(new TreeSet<>(Arrays.asList(roles)));
34  	}
35  
36  	public String getName() {
37  		return name;
38  	}
39  
40  	public void setName(String name) {
41  		this.name = name;
42  	}
43  
44  	public String getDisplayName() {
45  		return displayName;
46  	}
47  
48  	public void setDisplayName(String displayName) {
49  		this.displayName = displayName;
50  	}
51  
52  	public String getCmsSessionId() {
53  		return cmsSessionId;
54  	}
55  
56  	public void setCmsSessionId(String cmsSessionId) {
57  		this.cmsSessionId = cmsSessionId;
58  	}
59  
60  	public Boolean isAnonymous() {
61  		return name == null;
62  	}
63  
64  	public String getLocale() {
65  		return locale;
66  	}
67  
68  	public void setLocale(String locale) {
69  		this.locale = locale;
70  	}
71  
72  	@Override
73  	public boolean hasRole(String name) {
74  		return roles.contains(name);
75  	}
76  
77  	@Override
78  	public String[] getRoles() {
79  		return roles.toArray(new String[roles.size()]);
80  	}
81  
82  	public void setRoles(String[] roles) {
83  		this.roles = Collections.unmodifiableSortedSet(new TreeSet<>(Arrays.asList(roles)));
84  	}
85  
86  	@Override
87  	public int hashCode() {
88  		return cmsSessionId != null ? cmsSessionId.hashCode() : super.hashCode();
89  	}
90  
91  	@Override
92  	public String toString() {
93  		return displayName != null ? displayName : name != null ? name : super.toString();
94  	}
95  
96  }