Salome HOME
e9a77ed5f99448f1cd61bfa28656de6681de33a5
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / som / ApplicationRights.java
1 package org.splat.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012-2014
7  */
8
9 import java.util.HashSet;
10 import java.util.Set;
11
12 import org.splat.dal.bo.kernel.Role;
13 import org.splat.dal.bo.kernel.User;
14
15 public class ApplicationRights {
16
17         private static final String N_1 = "Nx1";
18         private static final String N_2 = "Nx2";
19         private transient final User _user;
20         private transient final Set<String> _roles;
21
22         // ==============================================================================================================================
23         // Construction
24         // ==============================================================================================================================
25
26         public ApplicationRights(final User user) { // Warning: user may be null
27                 this._roles = new HashSet<String>();
28                 this._user = user;
29                 if (user != null) {
30                         Role[] role = user.getRoles();
31                         for (int i = 0; i < role.length; i++) {
32                                 String iam = role[i].getName();
33                                 _roles.add(iam);
34                         }
35                 }
36         }
37
38         // ==============================================================================================================================
39         // Public member functions
40         // ==============================================================================================================================
41
42         public boolean canCreateStudy() {
43                 boolean res = (_user == null);
44                 if (res) {
45                         res = _roles.contains(Profile.manager.toString());
46                 } else {
47                         String position = _user.getOrganizationName();
48                         res = (position == null || !position.equals(N_2));
49                 }
50                 return res;
51         }
52
53         public boolean canContributeToStudy() {
54                 boolean res = (_user == null);
55                 if (res) {
56                         res = (_roles.contains(Profile.manager.toString()) || _roles
57                                         .contains(Profile.studengineer.toString()));
58                 } else {
59                         String position = _user.getOrganizationName();
60                         res = (position == null || ((!position.equals(N_1)) && (!position
61                                         .equals(N_2))));
62                 }
63                 return res;
64         }
65
66         public boolean canValidate() {
67                 return _roles.contains(Profile.manager.toString());
68         }
69
70         public boolean canManageKnowledges() {
71                 boolean res = (_user == null); 
72                 if (res) {
73                         res = _roles.contains(Profile.knowledgineer.toString());
74                 } else {
75                         String position = _user.getOrganizationName();
76                         res = (position == null || (!position.equals(N_2)));
77                 }
78                 return res;
79         }
80
81         public boolean canManageDatabase() {
82                 boolean res = (_user == null); 
83                 if (res) {
84                         res = _roles.contains(Profile.sysadmin.toString());
85                 } else {
86                         String position = _user.getOrganizationName();
87                         res = ((position == null || (!position.equals(N_2))) && _roles.contains(Profile.sysadmin.toString()));
88                 }
89                 return res;
90         }
91
92         // ==============================================================================================================================
93         // Getters
94         // ==============================================================================================================================
95
96         public User getUser() {
97                 return _user; // May be null
98         }
99 }