Subversion Repositories bacoAlunos

Rev

Rev 1616 | Rev 1814 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1312 jmachado 1
package pt.estgp.estgweb.domain;
2
 
3
import com.owlike.genson.Genson;
4
import org.apache.log4j.Logger;
5
import org.json.JSONArray;
6
import org.json.JSONException;
7
import pt.estgp.estgweb.services.pageContent.PageContentAccessPolicyAcl;
8
 
9
import java.io.IOException;
10
import java.util.ArrayList;
11
import java.util.HashMap;
12
import java.util.Map;
13
 
14
 
15
/**
16
 * Created by jorgemachado on 03/02/16.
17
 */
18
public abstract class PageContentImpl extends PageContent implements ClearanceRuled
19
{
20
 
21
    private static 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(PageContentImpl.class);
22
 
23
    private UserSession sess = null;
24
    private Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> userSessionClearences = null;
25
    private boolean childsInitialized = false;
26
    private boolean parentInitialized = false;
27
 
1616 jmachado 28
 
1312 jmachado 29
    public static class 1.5.0/docs/api/java/security/acl/Acl.html">Acl
30
    {
1616 jmachado 31
        public static 1.5.0/docs/api/java/security/acl/Acl.html">Acl AUTHENTICATED_ACL = new 1.5.0/docs/api/java/security/acl/Acl.html">Acl(1.5.0/docs/api/java/security/acl/Acl.html">Acl.Access.PERMIT.name(),
32
                1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/lang/reflect/Type.html">Type.AUTHENTICATED.name(),"",1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.READ_CONTENTS.name());
33
        public static ArrayList<Acl> AUTHENTICATED_ACL_SIMPLE_LIST = new ArrayList<Acl>();
34
        static{
35
            AUTHENTICATED_ACL_SIMPLE_LIST.add(AUTHENTICATED_ACL);
36
        }
37
 
38
        public 1.5.0/docs/api/java/security/acl/Acl.html">Acl(1.5.0/docs/api/java/lang/String.html">String access, 1.5.0/docs/api/java/lang/String.html">String type, 1.5.0/docs/api/java/lang/String.html">String id, 1.5.0/docs/api/java/lang/String.html">String operation) {
39
            this.access = access;
40
            this.type = type;
41
            this.id = id;
42
            this.operation = operation;
43
        }
44
 
45
        public 1.5.0/docs/api/java/security/acl/Acl.html">Acl() {
46
        }
47
 
1312 jmachado 48
        public static enum Access
49
        {
50
            PERMIT,
51
            DENY //default
52
        }
53
        public static enum 1.5.0/docs/api/java/lang/reflect/Type.html">Type
54
        {
55
            ALL,
56
            ANONYMOUS, //ONLY SUPER USER SHOULD ADD ANONYMOUS PERMISSIONS
57
            AUTHENTICATED,
58
            ROLE,
59
            USER,
60
            COURSEUNIT //SPECIFIC PERMISSIONS RELATIVE TO COURSE UNITS
61
        }
62
        public static enum 1.5.0/docs/api/java/rmi/server/Operation.html">Operation
63
        {
64
            ALL, //SPECIAL OPERATION FOR DENY AND PERMIT REPLACE FULL CONTROL
65
 
66
            READ_CONTENTS,
67
 
68
            READ_VERSIONS,
69
            READ_INVISIBLES,
70
 
71
 
72
            ORDER_CONTENTS,
73
 
1561 jmachado 74
            ADD_SECTIONS, //ADD TEXT USA ESTA PERMISSAO
1312 jmachado 75
            ADD_FILES,
76
            ADD_URLS,
77
            ADD_MODULES,
78
 
79
            REMOVE_SECTIONS,
80
            REMOVE_FILES,
81
            REMOVE_URLS,
82
            REMOVE_MODULES,
83
 
84
            EDIT_SECTIONS,
85
            EDIT_FILES,
86
            EDIT_URLS,
87
            EDIT_MODULES;
88
 
89
            public static 1.5.0/docs/api/java/rmi/server/Operation.html">Operation parse(1.5.0/docs/api/java/lang/String.html">String clearance)
90
            {
91
                if(clearance == null)
92
                    return null;
93
                for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation op: 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values())
94
                {
95
                    if(op.name().equals(clearance))
96
                        return op;
97
                }
98
                return null;
99
            }
1616 jmachado 100
 
101
 
1312 jmachado 102
        }
103
 
104
        public static enum CourseUnitsId
105
        {
106
            COURSE_UNIT_TEACHER("page.content.course.unit.teacher"),
107
            COURSE_UNIT_STUDENT("page.content.course.unit.student"),
108
            COURSE_UNIT_RESP_TEACHER("page.content.course.unit.resp.teacher"),
109
            COURSE_UNIT_COURSE_COMISSION("page.content.course.unit.course.comission");
110
 
111
            1.5.0/docs/api/java/lang/String.html">String msgKey;
112
 
113
 
114
            CourseUnitsId(1.5.0/docs/api/java/lang/String.html">String msgKey) {
115
                this.msgKey = msgKey;
116
            }
117
        }
118
 
119
        private 1.5.0/docs/api/java/lang/String.html">String access;
120
        private 1.5.0/docs/api/java/lang/String.html">String type;
121
        private 1.5.0/docs/api/java/lang/String.html">String id;
122
        private 1.5.0/docs/api/java/lang/String.html">String operation;
123
 
124
        public 1.5.0/docs/api/java/lang/String.html">String getAccess() {
125
            return access;
126
        }
127
 
128
        public void setAccess(1.5.0/docs/api/java/lang/String.html">String access) {
129
            this.access = access;
130
        }
131
 
132
        public void setAccessEnum(Access access) {
133
            this.access = access.name();
134
        }
135
 
136
        public 1.5.0/docs/api/java/lang/String.html">String getType() {
137
            return type;
138
        }
139
 
140
        public void setType(1.5.0/docs/api/java/lang/String.html">String type) {
141
            this.type = type;
142
        }
143
        public void setTypeEnum(1.5.0/docs/api/java/lang/reflect/Type.html">Type type) {
144
            this.type = type.name();
145
        }
146
 
147
        public 1.5.0/docs/api/java/lang/String.html">String getId() {
148
            return id;
149
        }
150
 
151
        public void setId(1.5.0/docs/api/java/lang/String.html">String id) {
152
            this.id = id;
153
        }
154
 
155
        public 1.5.0/docs/api/java/lang/String.html">String getOperation() {
156
            return operation;
157
        }
158
 
159
        public void setOperation(1.5.0/docs/api/java/lang/String.html">String operation) {
160
            this.operation = operation;
161
        }
162
 
163
        public void setOperationEnum(1.5.0/docs/api/java/rmi/server/Operation.html">Operation operation) {
164
            this.operation = operation.name();
165
        }
166
 
167
        public void setCourseUnitIdEnum(CourseUnitsId courseUnitIdEnum) {
168
            this.id = courseUnitIdEnum.name();
169
        }
170
 
171
        public Access getAccessEnum()
172
        {
173
            for(Access a : Access.values())
174
            {
175
                if(a.name().equals(getAccess()))
176
                    return a;
177
            }
178
            return Access.DENY;
179
        }
180
 
181
        public 1.5.0/docs/api/java/lang/reflect/Type.html">Type getTypeEnum()
182
        {
183
            for(1.5.0/docs/api/java/lang/reflect/Type.html">Type a : 1.5.0/docs/api/java/lang/reflect/Type.html">Type.values())
184
            {
185
                if(a.name().equals(getType()))
186
                    return a;
187
            }
188
            return 1.5.0/docs/api/java/lang/reflect/Type.html">Type.ROLE;
189
        }
190
 
191
        public 1.5.0/docs/api/java/rmi/server/Operation.html">Operation getOperationEnum()
192
        {
193
            for(1.5.0/docs/api/java/rmi/server/Operation.html">Operation a : 1.5.0/docs/api/java/rmi/server/Operation.html">Operation.values())
194
            {
195
                if(a.name().equals(getOperation()))
196
                    return a;
197
            }
198
            return null;
199
        }
200
 
201
        public CourseUnitsId getCourseUnitIdEnum()
202
        {
203
            for(CourseUnitsId a : CourseUnitsId.values())
204
            {
205
                if(a.name().equals(getId()))
206
                    return a;
207
            }
208
            return null;
209
        }
210
    }
211
 
212
    /**
213
     * Empty Array Means no ACL's defined
214
     * build a json array form ACL's
215
     * @return example [{"access":"PERMIT","operation":"FULL_CONTROL","id":"services","type":"ROLE"}]
216
     * @throws JSONException
217
     */
218
    public JSONArray getAclJson() throws JSONException
219
    {
220
        if(getAcl() == null || getAcl().trim().length() == 0)
221
            return new JSONArray();
222
        return new JSONArray(getAcl());
223
    }
224
 
225
    /**
226
     * Sets the string ACL with the proper json
227
     * @param json
228
     * @throws JSONException
229
     */
230
    public void setAclJson(JSONArray json) throws JSONException
231
    {
232
        if(json == null)
233
            setAcl(null);
234
        else
235
            setAcl(json.toString());
236
    }
237
 
238
    /**
239
     * sets the string Acl with the json built from given List
240
     * @param acls
241
     * @throws JSONException
242
     */
1776 jmachado 243
    public void setAclList(ArrayList<Acl> acls) throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException {
1312 jmachado 244
        if(acls == null || acls.size() == 0)
245
        {
246
            setAcl(null);
247
            return;
248
        }
249
        setAcl(getAclJson(acls).toString());
250
    }
251
 
252
    /**
253
     * Empty Array Means no ACL's defined allays
254
     * @return allays a list if no acls return empty list
255
     * @throws JSONException
256
     */
1776 jmachado 257
    public ArrayList<Acl> getAclList() throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException {
1312 jmachado 258
        ArrayList<Acl> acls = new ArrayList<Acl>();
259
        if(getAcl() == null || getAcl().trim().length() == 0)
260
            return acls;
261
        Genson genson = getGenson();
262
        ArrayList<HashMap<String,String>> aclsDes = genson.deserialize(getAcl(),1.5.0/docs/api/java/util/ArrayList.html">ArrayList.class);
263
        for(HashMap<String,String> acl: aclsDes)
264
        {
265
            1.5.0/docs/api/java/security/acl/Acl.html">Acl a = new 1.5.0/docs/api/java/security/acl/Acl.html">Acl();
266
            a.setType(acl.get("type"));
267
            a.setOperation(acl.get("operation"));
268
            a.setAccess(acl.get("access"));
269
            a.setId(acl.get("id"));
270
            acls.add(a);
271
        }
272
        return acls;
273
    }
274
 
275
    /**
276
     * Add a acl to String json at the end
277
     * @param acl
278
     * @throws IOException
279
     * @throws JSONException
280
     */
1776 jmachado 281
    public void addAcl(1.5.0/docs/api/java/security/acl/Acl.html">Acl acl) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
1312 jmachado 282
        if(acl == null)
283
            return;
284
        ArrayList<Acl> acls = getAclList();
285
        acls.add(acl);
286
        setAclList(acls);
287
    }
288
 
289
    private static Genson getGenson(){
290
        Genson genson = new Genson.Builder()
291
                .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
292
                .include("access", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
293
                .include("type", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
294
                .include("id", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
295
                .include("operation", 1.5.0/docs/api/java/security/acl/Acl.html">Acl.class)
296
                .create();
297
        return genson;
298
    }
299
 
300
    /**
301
     * Utilitário
302
     * @param aclList
303
     * @return
304
     * @throws IOException
305
     * @throws JSONException
306
     */
1776 jmachado 307
    public static JSONArray getAclJson(ArrayList<Acl> aclList) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
1312 jmachado 308
        if(aclList == null)
309
            return new JSONArray();
310
        Genson genson = getGenson();
311
        1.5.0/docs/api/java/lang/String.html">String json = genson.serialize(aclList);
312
        //System.out.println(json);
313
        return new JSONArray(json);
314
    }
315
 
316
 
317
    public boolean isChildsInitialized() {
318
        return childsInitialized;
319
    }
320
 
321
    public void setChildsInitialized(boolean childsInitialized) {
322
        this.childsInitialized = childsInitialized;
323
    }
324
 
325
    public boolean isParentInitialized() {
326
        return parentInitialized;
327
    }
328
 
329
    public void setParentInitialized(boolean parentInitialized) {
330
        this.parentInitialized = parentInitialized;
331
    }
332
 
333
    /**
334
     * Metodo de testes
335
     * @param args
336
     * @throws IOException
337
     * @throws JSONException
338
     */
1776 jmachado 339
    public static void main(1.5.0/docs/api/java/lang/String.html">String [] args) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException {
1312 jmachado 340
 
341
 
342
 
343
    }
344
 
345
    public boolean canAdmin(UserSessionImpl u)
346
    {
347
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
348
        {
349
            logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie());
350
            return false;
351
        }
352
        else if(userSessionClearences != null)
353
        {
354
            for(1.5.0/docs/api/java/util/Map.html">Map.Entry<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation,Boolean> entry: userSessionClearences.entrySet())
355
            {
356
                if(entry.getValue() == true &&
357
                        !entry.getKey().name().startsWith("READ"))
358
                {
359
                    return true;
360
                }
361
            }
362
            return false;
363
        }
364
        else
365
        {
366
            logger.warn("Checking clearance without sessionClearances initialized, not allowed");
367
            return false;
368
        }
369
    }
370
 
371
 
372
 
373
 
374
    public boolean isClear(UserSessionImpl u, 1.5.0/docs/api/java/lang/String.html">String OP)
375
    {
376
        1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation o = 1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation.parse(OP);
377
        return isClear(u,o);
378
    }
379
 
380
    /**
381
     * Same logic in isClearanceInitialized Method
382
     * @param u
383
     * @return
384
     */
385
    public boolean isClear(UserSessionImpl u, PageContentImpl.1.5.0/docs/api/java/security/acl/Acl.html">Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation op)
386
    {
387
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
388
        {
389
            logger.warn("Clearances checked for Mr:" + sess.getCookie() + " but clearing for Mr:" + u.getCookie());
390
            return false;
391
        }
1616 jmachado 392
        else if(sess == null || sess.getCookie() == null || u == null || u.getCookie() == null)
393
        {
394
            logger.warn("Clearances are being checked to a user or a session with no cookie identifier");
395
            return false;
396
        }
1312 jmachado 397
        else if(userSessionClearences != null)
398
        {
399
            return userSessionClearences.get(op);
400
        }
401
        else
402
        {
403
            logger.warn("Checking clearance without sessionClearances initialized, not allowed");
404
            return false;
405
        }
406
    }
407
 
408
    /**
409
     * Same logic in isClear Method
410
     * @param u
411
     * @return
412
     */
413
    public boolean isClearanceInitialized(UserSessionImpl u)
414
    {
415
        if(sess != null && sess.getCookie() != null && !sess.getCookie().equals(u.getCookie()))
416
        {
417
            return false;
418
        }
419
        else if(userSessionClearences == null)
420
        {
421
            return false;
422
        }
423
        return true;
424
    }
425
 
1326 jmachado 426
    @1.5.0/docs/api/java/lang/Override.html">Override
427
    public void initClearances(UserSessionImpl u, boolean force) {
1603 jmachado 428
        throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("NotImplemented");
1326 jmachado 429
    }
1312 jmachado 430
 
431
 
1326 jmachado 432
 
1312 jmachado 433
    public void setUserSessionClearences(Map<Acl.1.5.0/docs/api/java/rmi/server/Operation.html">Operation, Boolean> userSessionClearences) {
434
        this.userSessionClearences = userSessionClearences;
435
    }
436
 
437
    public void setSess(UserSession sess) {
438
        this.sess = sess;
439
    }
440
 
441
    @1.5.0/docs/api/java/lang/Override.html">Override
442
    public 1.5.0/docs/api/java/lang/String.html">String getAccessPolicyClass() {
443
        if(super.getAccessPolicyClass() == null)
444
        {
445
            logger.debug("No AccessPolicyClass in PageContent " + getId() + ":" + getTitle() + " using default:" + PageContentAccessPolicyAcl.class.getName());
446
            return PageContentAccessPolicyAcl.class.getName();
447
        }
448
        return super.getAccessPolicyClass();
449
    }
450
}