1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package net.sf.jguard.ext.authorization.manager;
29
30 import net.sf.jguard.core.authorization.manager.AuthorizationManager;
31 import java.security.Permission;
32 import java.security.Principal;
33 import java.sql.Connection;
34 import java.sql.PreparedStatement;
35 import java.sql.ResultSet;
36 import java.sql.SQLException;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Properties;
45 import java.util.Set;
46 import java.util.Stack;
47
48 import javax.sql.DataSource;
49
50 import net.sf.jguard.core.CoreConstants;
51 import net.sf.jguard.core.PolicyEnforcementPointOptions;
52 import net.sf.jguard.core.authorization.permissions.Domain;
53 import net.sf.jguard.core.authorization.permissions.JGPermissionCollection;
54 import net.sf.jguard.core.authorization.permissions.JGPositivePermissionCollection;
55 import net.sf.jguard.core.authorization.permissions.NoSuchPermissionException;
56 import net.sf.jguard.core.authorization.permissions.PermissionUtils;
57 import net.sf.jguard.core.principals.RolePrincipal;
58 import net.sf.jguard.ext.JdbcManager;
59 import net.sf.jguard.ext.JdbcManagerHelper;
60 import net.sf.jguard.ext.SecurityConstants;
61 import net.sf.jguard.core.authorization.AuthorizationException;
62 import net.sf.jguard.ext.database.ConnectionFactory;
63 import net.sf.jguard.ext.database.DatabaseUtils;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67
68
69
70
71
72
73 public class JdbcAuthorizationManager extends AbstractAuthorizationManager implements AuthorizationManager,JdbcManager{
74
75 private static final Logger logger = LoggerFactory.getLogger(JdbcAuthorizationManager.class.getName());
76
77
78 protected static final String NAME = "name";
79 protected static final String DOMAIN_NAME= "domain_name";
80
81
82 protected Map domainIds;
83
84
85 protected static String jgDomainSeq ="jg_domain_seq";
86 protected static String jgPermissionSeq ="jg_permission_seq";
87 protected static String jgAppPrincipalSeq ="jg_app_principal_seq";
88 protected static String jgUrlQuerySeq ="jg_url_query_seq";
89 protected static String jgPrincipalDomain ="jg_principal_domain";
90 protected static String jgDomain ="jg_domain";
91 protected static String jgAppPrincipal ="jg_app_principal";
92 protected static String jgPrincipalPermission ="jg_principal_permission";
93 protected static String jgPermission= "jg_permission";
94 protected static String jgUrlQuery = "jg_urlquery";
95 protected static String jgPrincipalHierarchy = "jg_principal_principal";
96
97
98 protected String PRINCIPALS_HIERARCHY = "PRINCIPALS_HIERARCHY";
99 protected String CREATE_PRINCIPAL_INHERITANCE = "CREATE_PRINCIPAL_INHERITANCE";
100 protected String DELETE_PRINCIPAL_INHERITANCE = "DELETE_PRINCIPAL_INHERITANCE";
101
102
103 protected String PRINCIPALS="PRINCIPALS";
104 protected String PERMISSIONS_FROM_PRINCIPAL="PERMISSIONS_FROM_PRINCIPAL";
105 protected String PERMISSIONS_FROM_DOMAINS="PERMISSIONS_FROM_DOMAINS";
106 protected String PERMISSIONS="PERMISSIONS";
107 protected String CREATE_PERMISSION="CREATE_PERMISSION";
108 protected String CREATE_DOMAIN="CREATE_DOMAIN";
109 protected String CREATE_PRINCIPAL_PERMISSION="CREATE_PRINCIPAL_PERMISSION";
110 protected String CREATE_PRINCIPAL_DOMAIN="CREATE_PRINCIPAL_DOMAIN";
111 protected String DOMAINS="DOMAINS";
112
113 protected String DELETE_DOMAIN="DELETE_DOMAIN";
114
115 protected String DELETE_PRINCIPAL_DOMAIN="DELETE_PRINCIPAL_DOMAIN";
116
117 protected String DELETE_DOMAIN_PRINCIPAL="DELETE_DOMAIN_PRINCIPAL";
118
119 protected String UPDATE_DOMAIN="UPDATE_DOMAIN";
120 protected String UPDATE_PERMISSION="UPDATE_PERMISSION";
121 protected String CHANGE_DOMAIN_PERMISSION="CHANGE_DOMAIN_PERMISSION";
122 protected String DELETE_PRINCIPAL="DELETE_PRINCIPAL";
123 protected String CREATE_PRINCIPAL="CREATE_PRINCIPAL";
124 protected String UPDATE_PRINCIPAL="UPDATE_PRINCIPAL";
125
126 protected String DELETE_PERMISSION="DELETE_PERMISSION";
127
128 protected String DELETE_PRINCIPAL_PERMISSION="DELETE_PRINCIPAL_PERMISSION";
129
130 protected String DELETE_PERMISSION_PRINCIPAL="DELETE_PERMISSION_PRINCIPAL";
131 protected String READ_PERMISSION_ID="READ_PERMISSION_ID";
132 protected String READ_DOMAIN_ID ="READ_DOMAIN_ID";
133 protected String READ_PRINCIPAL_ID ="READ_PRINCIPAL_ID";
134
135 private Properties props;
136
137 private ConnectionFactory connectionFactory = null;
138
139
140
141
142
143 public JdbcAuthorizationManager(Map options) {
144
145 super(options);
146
147
148 connectionFactory = new ConnectionFactory(options);
149 init(options,connectionFactory);
150
151 }
152
153
154
155
156
157
158
159
160 public JdbcAuthorizationManager(DataSource dataSource,Map options) {
161 super(options);
162
163 connectionFactory = new ConnectionFactory(dataSource);
164 init(options,connectionFactory);
165
166 }
167
168 private void init(Map options, ConnectionFactory connectionFactory){
169 super.options = options;
170 props = new Properties();
171
172 domainIds = new HashMap();
173
174 String applicationName= (String)options.get(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel());
175 this.setApplicationName(applicationName);
176
177 Map jdbcMap = new HashMap();
178
179 String dbPropertiesLocation = (String)options.get(CoreConstants.AUTHORIZATION_DATABASE_FILE_LOCATION);
180 jdbcMap.put(JdbcManagerHelper.DB_PROPERTIES_LOCATION,dbPropertiesLocation);
181 String createRequiredDatabaseEntities = (String)options.get(SecurityConstants.AUTHORIZATION_DATABASE_CREATE_REQUIRED_DATABASE_ENTITIES);
182 jdbcMap.put(JdbcManagerHelper.CREATE_REQUIRED_DATABASE_ENTITIES, createRequiredDatabaseEntities);
183 String importXmlDataKey = SecurityConstants.AUTHORIZATION_DATABASE_IMPORT_XML_DATA;
184 jdbcMap.put(JdbcManagerHelper.IMPORT_XML_DATA_KEY, importXmlDataKey);
185 String importXmlDataValue = (String)options.get(SecurityConstants.AUTHORIZATION_DATABASE_IMPORT_XML_DATA);
186 jdbcMap.put(JdbcManagerHelper.IMP0RT_XML_DATA_VALUE, importXmlDataValue);
187 jdbcMap.put(JdbcManagerHelper.XML_FILE_NAME, "jGuardPrincipalsPermissions.xml");
188 JdbcManagerHelper.jdbcInit(this,connectionFactory,props,jdbcMap);
189 init();
190 }
191
192
193
194
195
196 private void init() {
197
198 urlp = initPermissions();
199 principalsSet = initPrincipals();
200 Iterator itPrincipalsSet = principalsSet.iterator();
201
202
203 while(itPrincipalsSet.hasNext()){
204 RolePrincipal tempPrincipal = (RolePrincipal)itPrincipalsSet.next();
205 principals.put(tempPrincipal.getLocalName(),tempPrincipal);
206 }
207 }
208
209
210
211
212
213
214 public List getInitParameters() {
215
216 String[] authorizationParameters = {"authorizationUrl","authorizationLogin",
217 "authorizationPassword","authorizationDriver"};
218 return Arrays.asList(authorizationParameters);
219 }
220
221
222
223
224
225
226
227 private Set initPrincipals() {
228
229 Set ppals = new HashSet();
230 Map principalsMap = new HashMap();
231 PreparedStatement pst;
232 PreparedStatement pst2;
233 PreparedStatement pst3;
234 ResultSet rs;
235 ResultSet rs2;
236 ResultSet rs3;
237
238 Connection connection = null;
239 try {
240 connection = connectionFactory.getConnection();
241 pst= connection.prepareStatement(props.getProperty(PRINCIPALS));
242 rs = pst.executeQuery();
243
244 while(rs.next()){
245 RolePrincipal tempJGuardPrincipal = new RolePrincipal();
246 tempJGuardPrincipal.setName(this.applicationName+"#"+rs.getString(NAME));
247 String name = rs.getString(NAME);
248
249
250 Set domainNames = new HashSet();
251
252
253 pst3 = connection.prepareStatement(props.getProperty(PERMISSIONS_FROM_DOMAINS));
254 pst3.setString(1,name);
255 rs3 = pst3.executeQuery();
256
257
258 while(rs3.next()){
259 String domainName = rs3.getString(DOMAIN_NAME);
260 if(!domainNames.contains(domainName)){
261 domainNames.add(domainName);
262 logger.debug(" add domain "+domainName+" to principal "+tempJGuardPrincipal.getLocalName());
263 JGPermissionCollection domainTemp = (JGPermissionCollection)domains.get(domainName);
264 tempJGuardPrincipal.addDomain(domainTemp);
265 }
266
267 }
268
269
270 pst2 = connection.prepareStatement(props.getProperty(PERMISSIONS_FROM_PRINCIPAL));
271 pst2.setString(1,name);
272 rs2 = pst2.executeQuery();
273
274
275 while(rs2.next()){
276 String permissionName = rs2.getString(NAME);
277 Permission perm;
278 try {
279 perm = (Permission)urlp.getPermission(permissionName);
280 tempJGuardPrincipal.addPermission(perm);
281 } catch (NoSuchPermissionException e) {
282 logger.warn(" permission "+permissionName+" is not present in the JGPermissionCollection ");
283 }
284 }
285
286 ppals.add(tempJGuardPrincipal);
287 principalsMap.put(tempJGuardPrincipal.getLocalName(), tempJGuardPrincipal);
288 }
289
290
291
292 rs.close();
293 pst.close();
294
295 pst = connection.prepareStatement(props.getProperty(PRINCIPALS_HIERARCHY));
296 rs = pst.executeQuery();
297
298 RolePrincipal ascendantPrincipal = null;
299
300 while (rs.next()) {
301 String principalAscendantName = rs.getString(1);
302 String principalDescendantName = rs.getString(2);
303
304 if (ascendantPrincipal == null || !ascendantPrincipal.getLocalName().equals(principalAscendantName)) {
305 ascendantPrincipal = (RolePrincipal) principalsMap.get(principalAscendantName);
306 }
307
308 RolePrincipal descendant = (RolePrincipal)principalsMap.get(principalDescendantName);
309 ascendantPrincipal.getDescendants().add(descendant);
310
311 logger.info("Principal " + principalAscendantName + " inherites from principal " + principalDescendantName);
312 }
313
314 rs.close();
315 pst.close();
316
317 } catch (SQLException e) {
318 if (logger.isDebugEnabled()) {
319 logger.debug("initPrincipals in AuthorizationManager SQL ERROR "+ e.getMessage());
320 }
321 }finally{
322 try {
323 connection.close();
324 } catch (SQLException e1) {
325 logger.error( "listPrincipals()", e1);
326 }
327 }
328
329 return ppals;
330 }
331
332
333
334
335
336
337 private JGPermissionCollection initPermissions() {
338
339 JGPermissionCollection urlPc= new JGPositivePermissionCollection();
340 Permission tempPermission = null;
341 String tempPermissionName="";
342 Connection connection2 = null;
343 try {
344
345 connection2 = connectionFactory.getConnection();
346 PreparedStatement pst2 = connection2.prepareStatement(props.getProperty(DOMAINS));
347 ResultSet rs2 = pst2.executeQuery();
348 while(rs2.next()){
349 String domainName = rs2.getString(NAME);
350 Long domainId = new Long((rs2.getInt("id")));
351 JGPermissionCollection domain = new Domain(domainName);
352 domainsSet.add(domain);
353 domains.put(domainName,domain);
354 domainIds.put(domainName,domainId);
355 Connection connection = null;
356 try{
357 connection = connectionFactory.getConnection();
358 PreparedStatement pst = connection.prepareStatement(props.getProperty(PERMISSIONS));
359 pst.setLong(1,domainId.longValue());
360 ResultSet rs = pst.executeQuery();
361
362
363 while(rs.next()){
364
365
366 if(rs.getString(NAME).equals(tempPermissionName)==false){
367 if(tempPermission!=null){
368 urlPc.add(tempPermission);
369 }
370 String clazz = rs.getString(("class"));
371 String actions = rs.getString(("actions"));
372 try {
373 tempPermission = PermissionUtils.getPermission(clazz,rs.getString(NAME),actions);
374 } catch (ClassNotFoundException e) {
375 logger.warn(e.getMessage());
376 continue;
377 }
378 permissions.put(tempPermission.getName(),tempPermission);
379 permissionsSet.add(tempPermission);
380
381 ((JGPermissionCollection)domains.get(domainName)).add(tempPermission);
382
383 }
384 }
385
386
387 if(tempPermission!=null){
388 urlPc.add(tempPermission);
389 }
390 }finally{
391 connection.close();
392 }
393
394 }
395
396
397 } catch (SQLException e) {
398
399 if (logger.isDebugEnabled()) {
400 logger.debug("listPermissions() - initializePermissions in AuthorizationManager SQL ERROR "+ e.getMessage());
401 }
402
403 }finally{
404 try {
405 connection2.close();
406 } catch (SQLException e) {
407 throw new RuntimeException(e);
408 }
409 }
410
411 return urlPc;
412 }
413
414
415
416 public void createRequiredDatabaseEntities(Properties properties,ConnectionFactory connectionFactory) {
417
418
419 List tablesNames = new ArrayList();
420 tablesNames.add("JG_DOMAIN");
421 tablesNames.add("JG_APP_PRINCIPAL");
422 tablesNames.add("JG_PERMISSION");
423 tablesNames.add("JG_PRINCIPAL_PERMISSION");
424 tablesNames.add("JG_PRINCIPAL_DOMAIN");
425 tablesNames.add("JG_PRINCIPAL_HIERARCHY");
426
427 List sequencesNames = new ArrayList();
428 sequencesNames.add("JG_APP_PRINCIPAL_SEQ");
429 sequencesNames.add("JG_PERMISSION_SEQ");
430 sequencesNames.add("JG_DOMAIN_SEQ");
431
432 List foreignkeysNames = new ArrayList();
433 foreignkeysNames.add("FK_PERMISSION_PRINCIPAL");
434 foreignkeysNames.add("FK_PRINCIPAL_PERMISSION");
435 foreignkeysNames.add("FK_DOMAIN_PRINCIPAL");
436 foreignkeysNames.add("FK_PRINCIPAL_DOMAIN");
437 foreignkeysNames.add("FK_PERMISSION_DOMAIN");
438 foreignkeysNames.add("FK_PRINCIPAL_HIERARCHY_PRINCIPAL");
439 foreignkeysNames.add("FK_PRINCIPAL_HIERARCHY_PRINCIPAL2");
440
441
442 DatabaseUtils.createRequiredDatabaseEntities(properties,connectionFactory,sequencesNames,tablesNames,foreignkeysNames);
443
444 }
445
446
447
448
449
450
451
452
453
454
455 public void createPermission(Permission permission,String domainName) throws AuthorizationException {
456
457
458 Connection conn = null;
459 PreparedStatement pst = null;
460
461 try {
462 conn = connectionFactory.getConnection();
463 pst = conn.prepareStatement(props.getProperty(CREATE_PERMISSION));
464
465 pst.setString(1,permission.getName());
466 pst.setString(2,permission.getActions());
467 pst.setString(3,permission.getClass().getName());
468 Long domainId =(Long)domainIds.get(domainName);
469 pst.setLong(4,domainId.longValue());
470 pst.executeUpdate();
471
472
473 urlp.add(permission);
474 if (logger.isDebugEnabled()) {
475 logger.debug("createPermission() - " + permission
476 + " added!");
477 logger.debug("createPermission() - permissions: " + urlp);
478 }
479
480 JGPermissionCollection domain = (JGPermissionCollection)domains.get(domainName);
481 domain.add(permission);
482 permissions.put(permission.getName(),permission);
483 permissionsSet.add(permission);
484
485
486
487
488
489
490 this.updatePrincipals(permission);
491
492
493 } catch (SQLException e) {
494 logger.error( "createPermission(URLPermission, String)", e);
495 }finally{
496 try {
497 pst.close();
498 conn.close();
499 } catch (SQLException e1) {
500 logger.error( "createPermission(URLPermission, String)", e1);
501 }
502 }
503
504
505 }
506
507
508
509
510
511
512
513
514
515
516
517 public void updatePermission(String oldPermissionName, Permission perm, String newDomainName) throws AuthorizationException {
518
519
520 PreparedStatement pst;
521 Connection conn = null;
522
523 try {
524 conn = connectionFactory.getConnection();
525 conn.setAutoCommit(false);
526 pst = conn.prepareStatement(props.getProperty(UPDATE_PERMISSION));
527
528
529 pst.setString(1,perm.getName());
530
531 pst.setString(2,perm.getActions());
532
533 pst.setString(3,perm.getClass().getName());
534
535 pst.setLong(4,((Long)domainIds.get(newDomainName)).longValue());
536
537 pst.setString(5,oldPermissionName);
538 pst.executeUpdate();
539
540 JGPermissionCollection newDomain = (JGPermissionCollection)domains.get(newDomainName);
541
542
543 Iterator itDomains = domainsSet.iterator();
544 Permission oldPermission = (Permission) permissions.get(oldPermissionName);
545 while(itDomains.hasNext()){
546 JGPermissionCollection domain = (JGPermissionCollection)itDomains.next();
547
548 if(domain.containsPermission(oldPermission)){
549 domain.removePermission(oldPermission);
550
551
552
553 break;
554 }
555 }
556
557
558 urlp.removePermission(oldPermission);
559 urlp.add(perm);
560 permissions.remove(oldPermissionName);
561 permissions.put(perm.getName(),perm);
562 Iterator itPermissionsSet = permissionsSet.iterator();
563 while(itPermissionsSet.hasNext()){
564 Permission tempPerm = (Permission)itPermissionsSet.next();
565 if(tempPerm.getName().equals(oldPermissionName)){
566 permissionsSet.remove(tempPerm);
567 break;
568 }
569 }
570 permissionsSet.add(perm);
571
572 newDomain.add(perm);
573
574
575 this.updatePrincipals(perm);
576
577
578 conn.commit();
579 } catch (SQLException e) {
580 throw new AuthorizationException(e);
581 }finally{
582 try {
583 conn.close();
584 } catch (SQLException e1) {
585 logger.error( "updatePermission(String, URLPermission, String)", e1);
586 }
587 }
588 }
589
590
591
592
593
594
595
596
597 public void deletePermission(String permissionName) throws AuthorizationException {
598
599
600 PreparedStatement pst;
601 PreparedStatement pst2;
602 Connection conn = null;
603 try {
604 conn = connectionFactory.getConnection();
605 conn.setAutoCommit(false);
606
607
608 pst = conn.prepareStatement(props.getProperty(DELETE_PERMISSION_PRINCIPAL));
609 pst.setString(1,permissionName);
610 logger.debug(props.getProperty(DELETE_PERMISSION_PRINCIPAL));
611 logger.debug("permissionName="+permissionName);
612 pst.executeUpdate();
613 pst.close();
614
615 pst2 = conn.prepareStatement(props.getProperty(DELETE_PERMISSION));
616 pst2.setString(1,permissionName);
617 pst2.executeUpdate();
618
619 this.removePermissionFromPrincipals(permissionName);
620 Permission permission = null;
621 try {
622 permission = (Permission)urlp.getPermission(permissionName);
623 } catch (NoSuchPermissionException e) {
624 throw new AuthorizationException(e);
625 }
626 Domain domain = getDomain(permission);
627 domain.removePermission(permission);
628 permissions.remove(permission.getName());
629 permissionsSet.remove(permission);
630 urlp.removePermission(permission);
631
632 this.updatePrincipals(domain);
633
634 Iterator urlpIt = urlp.getPermissions().iterator();
635 while(urlpIt.hasNext()){
636 Permission tempPermission = (Permission)urlpIt.next();
637 if(tempPermission.getName().equals(permissionName)){
638
639 getDomain(tempPermission).removePermission(tempPermission);
640
641 urlpIt.remove();
642 break;
643 }
644
645 }
646 conn.commit();
647 } catch (SQLException e) {
648 logger.error( "deletePermission(String)", e);
649 }finally{
650 try {
651 conn.close();
652 } catch (SQLException e1) {
653 logger.error( "deletePermission(String)", e1);
654 }
655 }
656
657 }
658
659
660
661
662
663
664
665 public void createDomain(String domainName) throws AuthorizationException {
666
667 PreparedStatement pst;
668 PreparedStatement pst2;
669 ResultSet rs2;
670 Connection conn = null;
671 try {
672 conn = connectionFactory.getConnection();
673 pst = conn.prepareStatement(props.getProperty(CREATE_DOMAIN));
674 pst.setString(1,domainName);
675 pst.executeUpdate();
676 JGPermissionCollection newDomain = new Domain(domainName);
677 pst2 = conn.prepareStatement(props.getProperty(READ_DOMAIN_ID));
678 pst2.setString(1,domainName);
679 rs2 = pst2.executeQuery();
680 conn.commit();
681
682 rs2.next();
683 domains.put(domainName,newDomain);
684 domainsSet.add(newDomain);
685 domainIds.put(domainName,new Long(rs2.getLong("id")));
686
687 } catch (SQLException e) {
688 logger.error( "createDomain(String)", e);
689 }finally{
690 try {
691 conn.close();
692 } catch (SQLException e1) {
693 logger.error( "createDomain(String)", e1);
694 }
695 }
696
697 }
698
699
700
701
702
703
704
705 public void updateDomain(String newDomainName,String oldDomainName) throws AuthorizationException {
706
707 PreparedStatement pst;
708 PreparedStatement pst2;
709 ResultSet rs2;
710 Connection conn = null;
711 try {
712 conn = connectionFactory.getConnection();
713 pst = conn.prepareStatement(props.getProperty(UPDATE_DOMAIN));
714 pst.setString(1,newDomainName);
715 pst.setString(2,oldDomainName);
716 pst.executeUpdate();
717
718 Domain updatedDomain = ((Domain)domains.get(oldDomainName));
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742 domainsSet.remove(domains.get(oldDomainName));
743
744 updatedDomain.setName(newDomainName);
745
746 domains.remove(oldDomainName);
747
748 domainIds.remove(oldDomainName);
749
750 domains.put(newDomainName,updatedDomain);
751
752 pst2 = conn.prepareStatement(props.getProperty(READ_DOMAIN_ID));
753 pst2.setString(1,newDomainName);
754 rs2 = pst2.executeQuery();
755
756 rs2.next();
757 domainIds.put(newDomainName,new Long(rs2.getLong("id")));
758 domainsSet.add(domains.get(updatedDomain.getName()));
759
760
761 this.updatePrincipals(updatedDomain, oldDomainName);
762 } catch (SQLException e) {
763 logger.error( "updateDomain(String, String)", e);
764 }finally{
765 try {
766 conn.close();
767 } catch (SQLException e1) {
768 logger.error( "updateDomain(String, String)", e1);
769 }
770 }
771
772 }
773
774
775
776
777
778
779
780
781 public void deleteDomain(String domainName) throws AuthorizationException {
782 if(domains.get(domainName)== null){
783 throw new AuthorizationException(" this domain does not exists ");
784 }else if(((JGPermissionCollection)domains.get(domainName)).getPermissions().isEmpty()==false){
785 throw new AuthorizationException(" there are "+
786 ((JGPermissionCollection)domains.get(domainName)).getPermissions().size()+" permissions bound to this domain ");
787 }else{
788
789
790 PreparedStatement pst;
791 PreparedStatement pst2;
792 Connection conn = null;
793 try {
794 conn = connectionFactory.getConnection();
795
796 pst2 = conn.prepareStatement(props.getProperty(DELETE_DOMAIN_PRINCIPAL));
797 pst2.setString(1,domainName);
798 pst2.executeUpdate();
799
800 pst = conn.prepareStatement(props.getProperty(DELETE_DOMAIN));
801 pst.setString(1,domainName);
802 pst.executeUpdate();
803 domainIds.remove(((Domain)domains.get(domainName)).getName());
804 domainsSet.remove(domains.get(domainName));
805
806 domains.remove(domainName);
807
808
809 super.removeDomainFromPrincipals(domainName);
810 } catch (SQLException e) {
811 logger.error( "deleteDomain(String)", e);
812 }finally{
813 try {
814 conn.close();
815 } catch (SQLException e1) {
816 logger.error( "deleteDomain(String)", e1);
817 }
818 }
819 }
820 }
821
822
823
824
825
826
827
828
829
830 public void updatePrincipal(String oldPrincipalName, Principal principal) throws AuthorizationException {
831
832 PreparedStatement pst;
833 PreparedStatement pst2;
834 PreparedStatement pst3;
835 Connection conn = null;
836
837 try {
838 conn = connectionFactory.getConnection();
839
840 pst2 = conn.prepareStatement(props.getProperty(DELETE_PRINCIPAL_PERMISSION));
841 pst2.setString(1,oldPrincipalName);
842 logger.debug(props.getProperty(DELETE_PRINCIPAL_PERMISSION));
843 logger.debug(oldPrincipalName);
844 pst2.executeUpdate();
845
846
847 pst3 = conn.prepareStatement(props.getProperty(DELETE_PRINCIPAL_DOMAIN));
848 pst3.setString(1,oldPrincipalName);
849 pst3.executeUpdate();
850
851
852 pst = conn.prepareStatement(props.getProperty(UPDATE_PRINCIPAL));
853 pst.setString(1,getLocalName(principal));
854 pst.setString(2,oldPrincipalName);
855 pst.executeUpdate();
856 } catch (SQLException e) {
857 logger.error( "updatePrincipal(String, RolePrincipal)", e);
858 throw new AuthorizationException(e);
859 }finally{
860 try {
861 conn.close();
862 } catch (SQLException e1) {
863 throw new AuthorizationException(" connection cannot be closed ",e1);
864 }
865 }
866 if(principal.getClass().equals(RolePrincipal.class)){
867
868 boolean result= addDomainsAndPermissions((RolePrincipal)principal);
869 if(!result){
870
871 return;
872 }
873 }
874
875 Principal oldPal = (Principal)principals.remove(oldPrincipalName);
876 principalsSet.remove(oldPal);
877 principals.put(getLocalName(principal),principal);
878 principalsSet.add(principal);
879
880
881 }
882
883
884
885
886
887
888 private boolean addDomainsAndPermissions(RolePrincipal principal) throws AuthorizationException {
889
890
891 PreparedStatement pst4;
892 PreparedStatement pst5;
893 PreparedStatement pst6;
894 PreparedStatement pst7;
895 PreparedStatement pst8;
896 ResultSet rs5;
897 ResultSet rs6;
898 ResultSet rs7;
899 Connection conn = null;
900 try {
901 conn = connectionFactory.getConnection();
902
903 pst6 = conn.prepareStatement(props.getProperty(READ_PRINCIPAL_ID));
904
905 pst6.setString(1,getLocalName(principal));
906 rs6 = pst6.executeQuery();
907 long idPrincipal;
908 if(rs6.next()){
909 idPrincipal = rs6.getLong(1);
910 }else{
911
912 return false;
913 }
914
915 Set orphanedPermissions = principal.getOrphanedPermissions();
916 Iterator itOrphanedPermissions = orphanedPermissions.iterator();
917 long idPermission;
918
919 while(itOrphanedPermissions.hasNext()){
920 Permission perm = (Permission)itOrphanedPermissions.next();
921 pst5 = conn.prepareStatement(props.getProperty(READ_PERMISSION_ID));
922 pst5.setString(1,perm.getName());
923 rs5 = pst5.executeQuery();
924 rs5.next();
925 idPermission = rs5.getLong(1);
926
927 pst8 = conn.prepareStatement(props.getProperty(CREATE_PRINCIPAL_PERMISSION));
928 pst8.setLong(1,idPrincipal);
929 pst8.setLong(2,idPermission);
930 pst8.executeUpdate();
931 }
932
933
934 Set doms = principal.getDomains();
935 Iterator itDomains = doms.iterator();
936 long idDomain;
937 while(itDomains.hasNext()){
938 Domain dom = (Domain)itDomains.next();
939 pst7 = conn.prepareStatement(props.getProperty(READ_DOMAIN_ID));
940 pst7.setString(1,dom.getName());
941 rs7 = pst7.executeQuery();
942 rs7.next();
943 idDomain = rs7.getLong(1);
944
945 pst4 = conn.prepareStatement(props.getProperty(CREATE_PRINCIPAL_DOMAIN));
946 pst4.setLong(1,idPrincipal);
947 pst4.setLong(2,idDomain);
948 pst4.executeUpdate();
949 }
950 return true;
951 } catch (SQLException e) {
952 throw new AuthorizationException(" an SQLException has been raised in the addDomainsAndPermissions method ",e);
953 }finally{
954 try {
955 conn.close();
956 } catch (SQLException e1) {
957 throw new AuthorizationException(" connection cannot be closed ",e1);
958 }
959 }
960 }
961
962
963
964
965
966
967 public void deletePrincipal(Principal principal) throws AuthorizationException {
968
969 PreparedStatement pst;
970 PreparedStatement pst2;
971 PreparedStatement pst3;
972 Connection conn = null;
973 try {
974 conn = connectionFactory.getConnection();
975 pst = conn.prepareStatement(props.getProperty(DELETE_PRINCIPAL_PERMISSION));
976 pst.setString(1,getLocalName(principal));
977 pst.executeUpdate();
978
979 pst3 = conn.prepareStatement(props.getProperty(DELETE_PRINCIPAL_DOMAIN));
980 pst3.setString(1,getLocalName(principal));
981 pst3.executeUpdate();
982
983 pst2 = conn.prepareStatement(props.getProperty(DELETE_PRINCIPAL));
984 pst2.setString(1,getLocalName(principal));
985 pst2.executeUpdate();
986 principals.remove(getLocalName(principal));
987 RolePrincipal ppal = new RolePrincipal();
988 ppal.setLocalName(getLocalName(principal));
989 ppal.setApplicationName(applicationName);
990 principalsSet.remove(ppal);
991
992 } catch (SQLException e) {
993 logger.error( "deletePrincipal(String)", e);
994 }finally{
995 try {
996 conn.close();
997 } catch (SQLException e1) {
998 logger.error( "deletePrincipal(String)", e1);
999 }
1000 }
1001
1002 }
1003
1004
1005
1006
1007
1008
1009
1010
1011 public void changeDomainPermission(String permissionName, String newDomainName) throws AuthorizationException{
1012
1013 PreparedStatement pst;
1014 Connection conn = null;
1015 try {
1016 conn = connectionFactory.getConnection();
1017 conn.setAutoCommit(false);
1018 pst = conn.prepareStatement(props.getProperty(CHANGE_DOMAIN_PERMISSION));
1019 pst.setString(1,newDomainName);
1020 pst.setString(2,permissionName);
1021 pst.executeUpdate();
1022 Permission perm;
1023 try {
1024 perm = (Permission)urlp.getPermission(permissionName);
1025 } catch (NoSuchPermissionException e) {
1026 throw new AuthorizationException(e);
1027 }
1028 JGPermissionCollection oldDomain =getDomain(perm);
1029 oldDomain.removePermission(perm);
1030 JGPermissionCollection newDomain =(JGPermissionCollection)domains.get(newDomainName);
1031 newDomain.add(perm);
1032 conn.commit();
1033 } catch (SQLException e) {
1034 logger.error( "changeDomainPermission(String, String)", e);
1035 }finally{
1036 try {
1037 conn.close();
1038 } catch (SQLException e1) {
1039 logger.error( "changeDomainPermission(String, String)", e1);
1040 }
1041 }
1042
1043 }
1044
1045 public void createPrincipal(Principal principal) throws AuthorizationException{
1046
1047 PreparedStatement pst;
1048 Connection conn = null;
1049
1050 try {
1051 conn = connectionFactory.getConnection();
1052 pst = conn.prepareStatement(props.getProperty(CREATE_PRINCIPAL));
1053 pst.setString(1,getLocalName(principal));
1054 int result = pst.executeUpdate();
1055 if(result==0){
1056 throw new AuthorizationException("principal "+getLocalName(principal)+" has not been created");
1057 }
1058 } catch (SQLException e) {
1059 logger.error( "createPrincipal(RolePrincipal)", e);
1060 throw new AuthorizationException(e);
1061 }finally{
1062 try {
1063 conn.close();
1064 } catch (SQLException e1) {
1065 logger.error( "createPrincipal(RolePrincipal)", e1);
1066 throw new AuthorizationException(e1);
1067 }
1068 }
1069 if(principal.getClass().equals(RolePrincipal.class)){
1070 addDomainsAndPermissions((RolePrincipal)principal);
1071 }
1072 principals.put(getLocalName(principal),principal);
1073
1074 principalsSet.add(principal);
1075
1076 }
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089 public void addInheritance(String principalAscName, String principalDescName) throws AuthorizationException {
1090
1091 RolePrincipal principalAsc = (RolePrincipal) principals.get(principalAscName);
1092 RolePrincipal principalDesc = (RolePrincipal) principals.get(principalDescName);
1093
1094 if (principalAsc == null) {
1095 logger.error("Principal " + principalAscName + " not found!");
1096 throw new AuthorizationException("Principal " + principalAscName + " not found!");
1097 }
1098
1099 if (principalDesc == null) {
1100 logger.error("Principal " + principalDescName + " not found!");
1101 throw new AuthorizationException("Principal " + principalDescName + " not found!");
1102 }
1103
1104
1105 for (Iterator it = principalAsc.getDescendants().iterator(); it.hasNext(); ) {
1106 if (principalDesc.equals(it.next())) {
1107 logger.warn("Principal " + principalAscName + " is immediate ascendant of Principal " + principalDescName + "!");
1108 }
1109 }
1110
1111
1112
1113 Stack principalsToCheck = new Stack();
1114
1115 Stack principalsFromNextLevel = new Stack();
1116 principalsToCheck.addAll(principalDesc.getDescendants());
1117
1118 while (!principalsToCheck.isEmpty()) {
1119 RolePrincipal principal = (RolePrincipal) principalsToCheck.pop();
1120 if (principalAsc.equals(principal)) {
1121 logger.error("Principal " + principalAscName + " cannot inherit Principal "
1122 + principalDescName + " because " + principalDescName + " inherit "
1123 + principalAscName);
1124 throw new AuthorizationException("Principal " + principalAscName + " cannot inherit Principal "
1125 + principalDescName + " because " + principalDescName + " inherit "
1126 + principalAscName);
1127 }
1128
1129 principalsFromNextLevel.addAll(principal.getDescendants());
1130
1131
1132 if (principalsToCheck.isEmpty()) {
1133 principalsToCheck.addAll(principalsFromNextLevel);
1134
1135
1136 principalsFromNextLevel.clear();
1137 }
1138 }
1139
1140
1141 PreparedStatement pst;
1142 Connection conn = null;
1143 try {
1144 conn = connectionFactory.getConnection();
1145
1146 pst = conn.prepareStatement(props.getProperty(READ_PRINCIPAL_ID));
1147 pst.setString(1, principalAscName);
1148 ResultSet rs = pst.executeQuery();
1149 rs.next();
1150 int principalAscId = rs.getInt(1);
1151 rs.close();
1152
1153
1154 pst.setString(1, principalDescName);
1155 rs = pst.executeQuery();
1156 rs.next();
1157 int principalDescId = rs.getInt(1);
1158 rs.close();
1159 pst.close();
1160
1161
1162 pst = conn.prepareStatement(props.getProperty(CREATE_PRINCIPAL_INHERITANCE));
1163 pst.setInt(1, principalAscId);
1164 pst.setInt(2, principalDescId);
1165 pst.executeUpdate();
1166 pst.close();
1167
1168
1169 principalAsc.getDescendants().add(principalDesc);
1170
1171 } catch (SQLException e) {
1172 logger.error( "addInheritance(principalAscName, principalDescName)", e);
1173 } finally {
1174 try {
1175 conn.close();
1176 } catch (SQLException e1) {
1177 logger.error( "addInheritance(principalAscName, principalDescName)", e1);
1178 }
1179 }
1180
1181
1182 }
1183
1184
1185
1186
1187
1188
1189 public void deleteInheritance(String principalAscName, String principalDescName) throws AuthorizationException {
1190
1191 PreparedStatement pst;
1192 Connection conn = null;
1193 try {
1194 conn = connectionFactory.getConnection();
1195
1196 pst = conn.prepareStatement(props.getProperty(READ_PRINCIPAL_ID));
1197 pst.setString(1, principalAscName);
1198 ResultSet rs = pst.executeQuery();
1199 rs.next();
1200 int principalAscId = rs.getInt(1);
1201 rs.close();
1202
1203
1204 pst.setString(1, principalDescName);
1205 rs = pst.executeQuery();
1206 rs.next();
1207 int principalDescId = rs.getInt(1);
1208 rs.close();
1209 pst.close();
1210
1211
1212 pst = conn.prepareStatement(props.getProperty(DELETE_PRINCIPAL_INHERITANCE));
1213 pst.setInt(1, principalAscId);
1214 pst.setInt(2, principalDescId);
1215 pst.executeUpdate();
1216
1217 pst.close();
1218
1219 RolePrincipal principalAsc = (RolePrincipal) principals.get(principalAscName);
1220 principalAsc.getDescendants().remove(principals.get(principalDescName));
1221
1222 logger.info("Inheritance beteween principal " + principalAscName + " and " + principalDescName + " was been deleted.");
1223 } catch (SQLException e) {
1224 logger.error( "assemblyPrincipalHierarchy(RolePrincipal)", e);
1225 }finally{
1226 try {
1227 conn.close();
1228 } catch (SQLException e1) {
1229 logger.error( "assemblyPrincipalHierarchy(RolePrincipal)", e1);
1230 }
1231 }
1232 }
1233
1234
1235
1236
1237
1238 public void insertRequiredData(String xmlFileLocation){
1239
1240 Map options = new HashMap();
1241 options.put(CoreConstants.AUTHORIZATION_XML_FILE_LOCATION,xmlFileLocation);
1242 options.put(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel(),this.applicationName);
1243
1244
1245 try {
1246 AuthorizationManager authManager = new XmlAuthorizationManager(options);
1247 importAuthorizationManager(authManager);
1248 } catch (AuthorizationException e) {
1249 logger.error(" error importing AuthorizationManager with options "+options);
1250 logger.warn(e.getMessage());
1251 }
1252
1253 }
1254
1255 public boolean isEmpty() {
1256 List selectQueries = new ArrayList();
1257 selectQueries.add("PRINCIPALS");
1258 selectQueries.add("ALL_PERMISSIONS");
1259 selectQueries.add("DOMAINS");
1260 return DatabaseUtils.isEmpty(this.props,connectionFactory,selectQueries);
1261
1262 }
1263
1264 public void refresh() {
1265 init();
1266 }
1267
1268
1269 }