KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > managementtool > actions > AuthorizationSwitchManagementAction


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.managementtool.actions;
25
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.exolab.castor.jdo.Database;
35 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
36 import org.infoglue.cms.controllers.kernel.impl.simple.AccessRightController;
37 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService;
38 import org.infoglue.cms.controllers.kernel.impl.simple.GroupControllerProxy;
39 import org.infoglue.cms.controllers.kernel.impl.simple.RoleControllerProxy;
40 import org.infoglue.cms.controllers.kernel.impl.simple.UserControllerProxy;
41 import org.infoglue.cms.entities.management.AccessRight;
42 import org.infoglue.cms.entities.management.AccessRightGroup;
43 import org.infoglue.cms.entities.management.AccessRightGroupVO;
44 import org.infoglue.cms.entities.management.AccessRightRole;
45 import org.infoglue.cms.entities.management.AccessRightRoleVO;
46 import org.infoglue.cms.entities.management.AccessRightUser;
47 import org.infoglue.cms.entities.management.AccessRightUserVO;
48 import org.infoglue.cms.entities.management.impl.simple.AccessRightUserImpl;
49 import org.infoglue.cms.exception.SystemException;
50 import org.infoglue.cms.security.InfoGlueGroup;
51 import org.infoglue.cms.security.InfoGluePrincipal;
52 import org.infoglue.cms.security.InfoGlueRole;
53 import org.infoglue.cms.util.CmsPropertyHandler;
54
55 /**
56  * This class is responsible for checking and fixing errors when one changes authsystem from one to another.
57  */

58
59 public class AuthorizationSwitchManagementAction extends InfoGlueAbstractAction
60 {
61     private static final long serialVersionUID = 1L;
62     
63     private final static Logger logger = Logger.getLogger(AuthorizationSwitchManagementAction.class.getName());
64
65     private List JavaDoc invalidUsers = new ArrayList JavaDoc();
66     private List JavaDoc invalidRoles = new ArrayList JavaDoc();
67     private List JavaDoc invalidGroups = new ArrayList JavaDoc();
68
69     private List JavaDoc users = new ArrayList JavaDoc();
70     private List JavaDoc roles = new ArrayList JavaDoc();
71     private List JavaDoc groups = new ArrayList JavaDoc();
72
73     private List JavaDoc accessRights = new ArrayList JavaDoc();
74
75     private String JavaDoc userName;
76     private String JavaDoc roleName;
77     private String JavaDoc groupName;
78     
79     private String JavaDoc newUserName;
80     private String JavaDoc newRoleName;
81     private String JavaDoc newGroupName;
82     
83     public String JavaDoc doInputUser() throws Exception JavaDoc
84     {
85         this.users = UserControllerProxy.getController().getAllUsers();
86         this.accessRights = getAccessRightsUser();
87         
88         return INPUT + "User";
89     }
90
91     public String JavaDoc doInputRole() throws Exception JavaDoc
92     {
93         this.roles = RoleControllerProxy.getController().getAllRoles();
94         this.accessRights = getAccessRightsRole();
95         
96         return INPUT + "Role";
97     }
98
99     public String JavaDoc doInputGroup() throws Exception JavaDoc
100     {
101         this.groups = GroupControllerProxy.getController().getAllGroups();
102         this.accessRights = getAccessRightsGroup();
103         
104         return INPUT + "Group";
105     }
106
107     public String JavaDoc doUpdateUser() throws Exception JavaDoc
108     {
109         if(this.getInfoGluePrincipal().getIsAdministrator())
110             updateAccessRightsUser(userName, newUserName);
111         else
112             throw new SystemException("You are not allowed to perform this action.");
113             
114         return "successUser";
115     }
116
117     public String JavaDoc doUpdateRole() throws Exception JavaDoc
118     {
119         if(this.getInfoGluePrincipal().getIsAdministrator())
120             updateAccessRightsRole(roleName, newRoleName);
121         else
122             throw new SystemException("You are not allowed to perform this action.");
123         
124         return "successRole";
125     }
126
127     public String JavaDoc doUpdateGroup() throws Exception JavaDoc
128     {
129         if(this.getInfoGluePrincipal().getIsAdministrator())
130             updateAccessRightsGroup(groupName, newGroupName);
131         else
132             throw new SystemException("You are not allowed to perform this action.");
133         
134         return "successGroup";
135     }
136
137     public String JavaDoc doExecute() throws Exception JavaDoc
138     {
139         /*
140         this.invalidUsers = getInvalidAccessRightsUser();
141         this.invalidRoles = getInvalidAccessRightsRole();
142         this.invalidGroups = getInvalidAccessRightsGroup();
143         */

144         
145         return SUCCESS;
146     }
147
148
149     private List JavaDoc getAccessRightsUser() throws Exception JavaDoc
150     {
151         List JavaDoc accessRightUserList = new ArrayList JavaDoc();
152
153         Database db = CastorDatabaseService.getDatabase();
154         
155         db.begin();
156
157         try
158         {
159     
160             try
161             {
162                 accessRightUserList = AccessRightController.getController().getAccessRightUserList(userName, db);
163             }
164             catch(Exception JavaDoc e)
165             {
166                 e.printStackTrace();
167             }
168             
169             db.commit();
170         }
171         catch(Exception JavaDoc e)
172         {
173             logger.error("An error occurred so we should not complete the transaction:" + e, e);
174             db.rollback();
175             throw new SystemException(e.getMessage());
176         }
177         finally
178         {
179             db.close();
180         }
181         
182         return accessRightUserList;
183     }
184
185     private List JavaDoc getAccessRightsRole() throws Exception JavaDoc
186     {
187         List JavaDoc accessRightRoleList = new ArrayList JavaDoc();
188
189         Database db = CastorDatabaseService.getDatabase();
190         
191         db.begin();
192
193         try
194         {
195             try
196             {
197                 accessRightRoleList = AccessRightController.getController().getAccessRightRoleList(roleName, db, false);
198             }
199             catch(Exception JavaDoc e)
200             {
201                 logger.error("An error occurred so we should not complete the transaction:" + e, e);
202             }
203             
204             db.commit();
205         }
206         catch(Exception JavaDoc e)
207         {
208             logger.error("An error occurred so we should not complete the transaction:" + e, e);
209             try
210             {
211                 db.rollback();
212             }
213             catch(Exception JavaDoc e2)
214             {
215                 logger.error("An error occurred so we should not complete the transaction:" + e2.getMessage());
216             }
217             throw new SystemException(e.getMessage());
218         }
219         finally
220         {
221             try
222             {
223                 db.close();
224             }
225             catch(Exception JavaDoc e2)
226             {
227                 logger.error("An error occurred so we should not complete the transaction:" + e2.getMessage());
228             }
229         }
230         
231         return accessRightRoleList;
232     }
233
234     private List JavaDoc getAccessRightsGroup() throws Exception JavaDoc
235     {
236         List JavaDoc accessRightGroupList = new ArrayList JavaDoc();
237
238         Database db = CastorDatabaseService.getDatabase();
239         
240         db.begin();
241
242         try
243         {
244     
245             try
246             {
247                 accessRightGroupList = AccessRightController.getController().getAccessRightGroupList(groupName, db);
248             }
249             catch(Exception JavaDoc e)
250             {
251                 e.printStackTrace();
252             }
253             
254             db.commit();
255         }
256         catch(Exception JavaDoc e)
257         {
258             logger.error("An error occurred so we should not complete the transaction:" + e, e);
259             db.rollback();
260             throw new SystemException(e.getMessage());
261         }
262         finally
263         {
264             db.close();
265         }
266         
267         return accessRightGroupList;
268     }
269
270     
271     private List JavaDoc getInvalidAccessRightsUser() throws Exception JavaDoc
272     {
273         Set JavaDoc invalidUsers = new HashSet JavaDoc();
274
275         Database db = CastorDatabaseService.getDatabase();
276         
277         db.begin();
278
279         try
280         {
281     
282             try
283             {
284                 List JavaDoc users = UserControllerProxy.getController(db).getAllUsers();
285                 List JavaDoc systemUserVOList = AccessRightController.getController().getAccessRightUserVOList(db);
286                 
287                 Iterator JavaDoc i = systemUserVOList.iterator();
288                 
289                 while(i.hasNext())
290                 {
291                     AccessRightUserVO accessRightUserVO = (AccessRightUserVO)i.next();
292                     
293                     boolean isValid = false;
294                     
295                     Iterator JavaDoc userIterator = users.iterator();
296                     
297                     while(userIterator.hasNext())
298                     {
299                         InfoGluePrincipal principal = (InfoGluePrincipal)userIterator.next();
300                         if(principal.getName().equalsIgnoreCase(accessRightUserVO.getUserName()))
301                         {
302                             isValid = true;
303                             break;
304                         }
305                     }
306                     
307                     if(!isValid)
308                         invalidUsers.add(accessRightUserVO.getUserName());
309                 }
310             }
311             catch(Exception JavaDoc e)
312             {
313                 e.printStackTrace();
314             }
315             
316             db.commit();
317         }
318         catch(Exception JavaDoc e)
319         {
320             logger.error("An error occurred so we should not complete the transaction:" + e, e);
321             db.rollback();
322             throw new SystemException(e.getMessage());
323         }
324         finally
325         {
326             db.close();
327         }
328         
329         List JavaDoc invalidUsersList = new ArrayList JavaDoc();
330         invalidUsersList.addAll(invalidUsers);
331
332         return invalidUsersList;
333     }
334
335     private List JavaDoc getInvalidAccessRightsRole() throws Exception JavaDoc
336     {
337         Set JavaDoc invalidRoles = new HashSet JavaDoc();
338
339         Database db = CastorDatabaseService.getDatabase();
340         
341         db.begin();
342
343         try
344         {
345     
346             String JavaDoc name = "AccessRightRole names";
347             String JavaDoc description = "Checks if the Role names given exists in the current authorizationModule.";
348     
349             try
350             {
351                 List JavaDoc users = RoleControllerProxy.getController(db).getAllRoles();
352                 List JavaDoc systemRoleVOList = AccessRightController.getController().getAccessRightRoleVOList(db);
353                 
354                 Iterator JavaDoc i = systemRoleVOList.iterator();
355                 
356                 while(i.hasNext())
357                 {
358                     AccessRightRoleVO accessRightRoleVO = (AccessRightRoleVO)i.next();
359                     
360                     boolean isValid = false;
361                     
362                     Iterator JavaDoc userIterator = users.iterator();
363                     
364                     while(userIterator.hasNext())
365                     {
366                         InfoGlueRole role = (InfoGlueRole)userIterator.next();
367                         if(role.getName().equalsIgnoreCase(accessRightRoleVO.getRoleName()))
368                         {
369                             isValid = true;
370                             break;
371                         }
372                     }
373                     
374                     if(!isValid)
375                         invalidRoles.add(accessRightRoleVO.getRoleName());
376                 }
377             }
378             catch(Exception JavaDoc e)
379             {
380                 e.printStackTrace();
381             }
382             
383             db.commit();
384         }
385         catch(Exception JavaDoc e)
386         {
387             logger.error("An error occurred so we should not complete the transaction:" + e, e);
388             db.rollback();
389             throw new SystemException(e.getMessage());
390         }
391         finally
392         {
393             db.close();
394         }
395
396         List JavaDoc invalidRolesList = new ArrayList JavaDoc();
397         invalidRolesList.addAll(invalidRoles);
398
399         return invalidRolesList;
400     }
401
402     private List JavaDoc getInvalidAccessRightsGroup() throws Exception JavaDoc
403     {
404         Set JavaDoc invalidGroups = new HashSet JavaDoc();
405
406         Database db = CastorDatabaseService.getDatabase();
407         
408         db.begin();
409
410         try
411         {
412     
413             String JavaDoc name = "AccessRightGroup names";
414             String JavaDoc description = "Checks if the user names given exists in the current authorizationModule.";
415     
416             try
417             {
418                 List JavaDoc users = GroupControllerProxy.getController(db).getAllGroups();
419                 List JavaDoc systemGroupVOList = AccessRightController.getController().getAccessRightGroupVOList(db);
420                 
421                 Iterator JavaDoc i = systemGroupVOList.iterator();
422                 
423                 while(i.hasNext())
424                 {
425                     AccessRightGroupVO accessRightGroupVO = (AccessRightGroupVO)i.next();
426                     
427                     boolean isValid = false;
428                     
429                     Iterator JavaDoc userIterator = users.iterator();
430                     
431                     while(userIterator.hasNext())
432                     {
433                         InfoGlueGroup group = (InfoGlueGroup)userIterator.next();
434                         if(group.getName().equalsIgnoreCase(accessRightGroupVO.getGroupName()))
435                         {
436                             isValid = true;
437                             break;
438                         }
439                     }
440                     
441                     if(!isValid)
442                         invalidGroups.add(accessRightGroupVO.getGroupName());
443                 }
444             }
445             catch(Exception JavaDoc e)
446             {
447                 e.printStackTrace();
448             }
449             
450             db.commit();
451         }
452         catch(Exception JavaDoc e)
453         {
454             logger.error("An error occurred so we should not complete the transaction:" + e, e);
455             db.rollback();
456             throw new SystemException(e.getMessage());
457         }
458         finally
459         {
460             db.close();
461         }
462
463         List JavaDoc invalidGroupsList = new ArrayList JavaDoc();
464         invalidGroupsList.addAll(invalidGroupsList);
465
466         return invalidGroupsList;
467     }
468     
469     private void updateAccessRightsUser(String JavaDoc userName, String JavaDoc newUserName) throws Exception JavaDoc
470     {
471         Database db = CastorDatabaseService.getDatabase();
472         
473         db.begin();
474
475         try
476         {
477             try
478             {
479                 List JavaDoc accessRightUserList = AccessRightController.getController().getAccessRightUserList(userName, db);
480                 
481                 Iterator JavaDoc i = accessRightUserList.iterator();
482                 
483                 while(i.hasNext())
484                 {
485                     AccessRightUser accessRightUser = (AccessRightUser)i.next();
486                     AccessRight accessRight = accessRightUser.getAccessRight();
487
488                     boolean exists = false;
489                     Iterator JavaDoc usersIterator = accessRight.getUsers().iterator();
490                     while(usersIterator.hasNext())
491                     {
492                         AccessRightUser currentAccessRightUser = (AccessRightUser)usersIterator.next();
493                         if(currentAccessRightUser.getUserName().equals(newUserName))
494                             exists = true;
495                     }
496
497                     if(!exists)
498                     {
499                         //accessRightUser.setUserName(newUserName);
500
AccessRightUserVO accessRightUserVO = new AccessRightUserVO();
501                         accessRightUserVO.setUserName(newUserName);
502                         AccessRightUser newAccessRightUser = AccessRightController.getController().createAccessRightUser(db, accessRightUserVO, accessRight);
503                         accessRight.getUsers().add(newAccessRightUser);
504                     }
505                 }
506             }
507             catch(Exception JavaDoc e)
508             {
509                 e.printStackTrace();
510             }
511             
512             db.commit();
513         }
514         catch(Exception JavaDoc e)
515         {
516             logger.error("An error occurred so we should not complete the transaction:" + e, e);
517             db.rollback();
518             throw new SystemException(e.getMessage());
519         }
520         finally
521         {
522             db.close();
523         }
524     }
525
526     private void updateAccessRightsRole(String JavaDoc roleName, String JavaDoc newRoleName) throws Exception JavaDoc
527     {
528         Database db = CastorDatabaseService.getDatabase();
529         
530         db.begin();
531
532         try
533         {
534             try
535             {
536                 List JavaDoc accessRightRoleList = AccessRightController.getController().getAccessRightRoleList(roleName, db, false);
537                 
538                 Iterator JavaDoc i = accessRightRoleList.iterator();
539                 
540                 while(i.hasNext())
541                 {
542                     AccessRightRole accessRightRole = (AccessRightRole)i.next();
543                     AccessRight accessRight = accessRightRole.getAccessRight();
544                     
545                     if(accessRight != null)
546                     {
547                         boolean exists = false;
548
549                         Iterator JavaDoc rolesIterator = accessRight.getRoles().iterator();
550                         while(rolesIterator.hasNext())
551                         {
552                             AccessRightRole currentAccessRightRole = (AccessRightRole)rolesIterator.next();
553                             if(currentAccessRightRole.getRoleName().equals(newRoleName))
554                                 exists = true;
555                         }
556                     
557                         if(!exists)
558                         {
559                             //accessRightRole.setRoleName(newRoleName);
560
AccessRightRoleVO accessRightRoleVO = new AccessRightRoleVO();
561                             accessRightRoleVO.setRoleName(newRoleName);
562                             AccessRightRole newAccessRightRole = AccessRightController.getController().createAccessRightRole(db, accessRightRoleVO, accessRight);
563                             accessRight.getRoles().add(newAccessRightRole);
564                         }
565                     }
566                 }
567             }
568             catch(Exception JavaDoc e)
569             {
570                 e.printStackTrace();
571             }
572             
573             db.commit();
574         }
575         catch(Exception JavaDoc e)
576         {
577             logger.error("An error occurred so we should not complete the transaction:" + e, e);
578             db.rollback();
579             throw new SystemException(e.getMessage());
580         }
581         finally
582         {
583             db.close();
584         }
585     }
586
587     private void updateAccessRightsGroup(String JavaDoc groupName, String JavaDoc newGroupName) throws Exception JavaDoc
588     {
589         Database db = CastorDatabaseService.getDatabase();
590         
591         db.begin();
592
593         try
594         {
595             try
596             {
597                 List JavaDoc accessRightGroupList = AccessRightController.getController().getAccessRightGroupList(groupName, db);
598                 
599                 Iterator JavaDoc i = accessRightGroupList.iterator();
600                 
601                 while(i.hasNext())
602                 {
603                     AccessRightGroup accessRightGroup = (AccessRightGroup)i.next();
604                     AccessRight accessRight = accessRightGroup.getAccessRight();
605
606                     boolean exists = false;
607                     Iterator JavaDoc groupsIterator = accessRight.getGroups().iterator();
608                     while(groupsIterator.hasNext())
609                     {
610                         AccessRightGroup currentAccessRightGroup = (AccessRightGroup)groupsIterator.next();
611                         if(currentAccessRightGroup.getGroupName().equals(newGroupName))
612                             exists = true;
613                     }
614                     
615                     if(!exists)
616                     {
617                         //accessRightGroup.setGroupName(newGroupName);
618
AccessRightGroupVO accessRightGroupVO = new AccessRightGroupVO();
619                         accessRightGroupVO.setGroupName(newGroupName);
620                         AccessRightGroup newAccessRightGroup = AccessRightController.getController().createAccessRightGroup(db, accessRightGroupVO, accessRight);
621                         accessRight.getGroups().add(newAccessRightGroup);
622                     }
623                 }
624             }
625             catch(Exception JavaDoc e)
626             {
627                 e.printStackTrace();
628             }
629             
630             db.commit();
631         }
632         catch(Exception JavaDoc e)
633         {
634             logger.error("An error occurred so we should not complete the transaction:" + e, e);
635             db.rollback();
636             throw new SystemException(e.getMessage());
637         }
638         finally
639         {
640             db.close();
641         }
642     }
643
644     public List JavaDoc getInvalidGroups()
645     {
646         return invalidGroups;
647     }
648
649     public List JavaDoc getInvalidRoles()
650     {
651         return invalidRoles;
652     }
653
654     public List JavaDoc getInvalidUsers()
655     {
656         return invalidUsers;
657     }
658
659     public String JavaDoc getGroupName()
660     {
661         return groupName;
662     }
663
664     public void setGroupName(String JavaDoc groupName)
665     {
666         this.groupName = groupName;
667     }
668
669     public String JavaDoc getRoleName()
670     {
671         return roleName;
672     }
673
674     public void setRoleName(String JavaDoc roleName)
675     {
676         this.roleName = roleName;
677     }
678
679     public String JavaDoc getUserName()
680     {
681         return userName;
682     }
683
684     public void setUserName(String JavaDoc userName)
685     {
686         this.userName = userName;
687     }
688
689     public String JavaDoc getNewGroupName()
690     {
691         return newGroupName;
692     }
693
694     public void setNewGroupName(String JavaDoc newGroupName)
695     {
696         this.newGroupName = newGroupName;
697     }
698
699     public String JavaDoc getNewRoleName()
700     {
701         return newRoleName;
702     }
703
704     public void setNewRoleName(String JavaDoc newRoleName)
705     {
706         this.newRoleName = newRoleName;
707     }
708
709     public String JavaDoc getNewUserName()
710     {
711         return newUserName;
712     }
713
714     public void setNewUserName(String JavaDoc newUserName)
715     {
716         this.newUserName = newUserName;
717     }
718
719     public List JavaDoc getGroups()
720     {
721         return groups;
722     }
723
724     public List JavaDoc getRoles()
725     {
726         return roles;
727     }
728
729     public List JavaDoc getUsers()
730     {
731         return users;
732     }
733
734     public List JavaDoc getAccessRights()
735     {
736         return accessRights;
737     }
738
739 }
740
Popular Tags