001: package org.osbl.authorization.gui;
002:
003: import org.osbl.client.web.spring.SpringWebModule;
004: import org.osbl.client.web.WebModule;
005: import org.osbl.client.wings.form.Editors;
006: import org.osbl.client.wings.action.ShowEditorAction;
007: import org.osbl.client.wings.navigation.NavigationStructure;
008: import org.osbl.client.action.ActionRegistry;
009: import org.osbl.plugin.PluginManager;
010: import org.osbl.plugin.Extension;
011: import org.osbl.ItemRegistry;
012: import org.osbl.ServiceProvider;
013: import org.osbl.identity.model.User;
014: import org.osbl.identity.model.Identity;
015: import org.osbl.persistence.*;
016: import org.osbl.authorization.model.*;
017: import org.osbl.item.BeanAuthorizationCollector;
018: import org.osbl.item.BeanLocalizationCollector;
019: import org.conform.hibernate.HibernateEnvironment;
020: import org.wings.session.SessionManager;
021:
022: import javax.swing.*;
023: import java.awt.event.ActionEvent;
024: import java.util.List;
025: import java.util.ArrayList;
026: import java.sql.Timestamp;
027:
028: public class AuthorizationWebModule implements WebModule {
029: PluginManager pluginManager;
030: ItemRegistry authorizationItems;
031: ItemRegistry localizationItems;
032:
033: public void setPluginManager(PluginManager pluginManager) {
034: this .pluginManager = pluginManager;
035: }
036:
037: public void setAuthorizationItems(ItemRegistry authorizationItems) {
038: this .authorizationItems = authorizationItems;
039: }
040:
041: public void setLocalizationItems(ItemRegistry localizationItems) {
042: this .localizationItems = localizationItems;
043: }
044:
045: public void initialize() {
046: authorizationItems.addItems(new BeanAuthorizationCollector(
047: Profile.class));
048: authorizationItems.addItems(new BeanAuthorizationCollector(
049: Pattern.class));
050:
051: localizationItems.addItems(new BeanLocalizationCollector(
052: Profile.class));
053: localizationItems.addItems(new BeanLocalizationCollector(
054: Pattern.class));
055:
056: //pluginManager.registerExtension(new Extension("AuthorizationSubForm", "org.osbl.identity.subForm", AuthorizationSubForm.class));
057:
058: Editors.registerEditor(Profile.class, ProfileEditor.class);
059:
060: ActionRegistry.registerAction(new ShowEditorAction(
061: Profile.class,
062: "org.osbl.authorization.actions.manageProfiles"));
063:
064: NavigationStructure navigationStructure = NavigationStructure
065: .getInstance();
066: navigationStructure.addAction("administration",
067: "org.osbl.authorization.actions.manageProfiles");
068:
069: pluginManager.registerExtension(new Extension(
070: "Create Admin Profile",
071: "org.osbl.client.wings.devtools",
072: CreateAdminProfileDevTool.class));
073: }
074:
075: public static class CreateAdminProfileDevTool extends
076: AbstractAction {
077: public CreateAdminProfileDevTool() {
078: putValue(Action.NAME, "Create Admin Profile");
079: }
080:
081: public void actionPerformed(ActionEvent e) {
082: try {
083: HibernateEnvironment.getInstance().beginTransaction();
084: Persistence persistence = (Persistence) ServiceProvider
085: .getInstance().getService(
086: "AuthorizationPersistence");
087: JoinedQueryCommand queryCommand = (JoinedQueryCommand) persistence
088: .createCommand("list");
089: queryCommand.setType(Profile.class);
090: queryCommand.addFilter("key", Operator.EQUAL, "admin");
091: Profile profile = profile((List<Profile>) queryCommand
092: .execute());
093:
094: List<Pattern> patterns = new ArrayList<Pattern>();
095: patterns.add(new Pattern(PatternType.INCLUDE, "r",
096: "org.*"));
097: patterns.add(new Pattern(PatternType.INCLUDE, "w",
098: "org.*"));
099: patterns.add(new Pattern(PatternType.INCLUDE, "x",
100: "org.*"));
101: profile.setPatterns(patterns);
102: SaveOrUpdateCommand saveCommand = (SaveOrUpdateCommand) persistence
103: .createCommand("save");
104: saveCommand.setObject(profile);
105: saveCommand.execute();
106:
107: persistence = (Persistence) ServiceProvider
108: .getInstance()
109: .getService("IdentityPersistence");
110: queryCommand = (JoinedQueryCommand) persistence
111: .createCommand("list");
112: queryCommand.clearFilters();
113: queryCommand.setType(User.class);
114: queryCommand.addFilter("account", Operator.EQUAL,
115: getUserId());
116: User user = user((List<User>) queryCommand.execute());
117:
118: Identity identity;
119: if (user.getGeneral() == null) {
120: queryCommand.clearFilters();
121: queryCommand.setType(Identity.class);
122: queryCommand.addFilter("key", Operator.EQUAL,
123: getUserId());
124: identity = identity((List<Identity>) queryCommand
125: .execute());
126: identity.addQualification("User");
127: user.setGeneral(identity);
128: } else
129: identity = user.getGeneral();
130:
131: List<Profile> profiles = user.getProfiles();
132: if (profiles == null) {
133: profiles = new ArrayList<Profile>();
134: user.setProfiles(profiles);
135: }
136: profiles.add(profile);
137:
138: SaveOrUpdateGeneralCommand saveGeneralCommand = (SaveOrUpdateGeneralCommand) persistence
139: .createCommand("save");
140: saveGeneralCommand.setObject(identity);
141: saveGeneralCommand.execute();
142: } finally {
143: HibernateEnvironment.getInstance().endTransaction();
144: }
145: }
146:
147: private User user(List<User> users) {
148: User user;
149: if (users.size() == 1)
150: user = users.get(0);
151: else {
152: user = new User();
153: user.setAccount("admin");
154: user.setPassword("admin".toCharArray());
155: user.setLocale(SessionManager.getSession().getLocale());
156: }
157: return user;
158: }
159:
160: private Identity identity(List<Identity> identities) {
161: Identity identity;
162: if (identities.size() == 1)
163: identity = identities.get(0);
164: else {
165: identity = new Identity();
166: identity.setKey(getUserId());
167: identity.setCreated(new Timestamp(System
168: .currentTimeMillis()));
169: identity.setCreatedBy(getUserId());
170: }
171: return identity;
172: }
173:
174: private Profile profile(List<Profile> profiles) {
175: Profile profile;
176: if (profiles.size() == 1)
177: profile = profiles.get(0);
178: else {
179: profile = new Profile();
180: profile.setKey("admin");
181: profile.setName("Admin");
182: profile.setCreated(new Timestamp(System
183: .currentTimeMillis()));
184: profile.setCreatedBy(getUserId());
185: }
186: return profile;
187: }
188:
189: private String getUserId() {
190: return SessionManager.getSession().getServletRequest()
191: .getUserPrincipal().getName();
192: }
193: }
194: }
|