001: package com.technoetic.xplanner.security.install;
002:
003: import java.util.Date;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import net.sf.hibernate.Hibernate;
008: import net.sf.hibernate.HibernateException;
009: import net.sf.hibernate.Session;
010: import org.apache.log4j.Logger;
011: import com.tacitknowledge.util.migration.MigrationContext;
012: import com.tacitknowledge.util.migration.MigrationException;
013:
014: import com.technoetic.xplanner.db.hibernate.GlobalSessionFactory;
015: import com.technoetic.xplanner.db.hibernate.HibernateHelper;
016: import com.technoetic.xplanner.db.hsqldb.HsqlServer;
017: import com.technoetic.xplanner.domain.Person;
018: import com.technoetic.xplanner.domain.Role;
019: import com.technoetic.xplanner.domain.RoleAssociation;
020: import com.technoetic.xplanner.security.auth.Permission;
021:
022: public class BootstrapSystemUser {
023: private Logger log = Logger.getLogger(getClass());
024: protected static final String SYSADMIN_USER_ID = "sysadmin";
025: protected static final int PATCH_LEVEL = 2;
026: protected static final String PATCH_NAME = "XPlanner bootstrap";
027:
028: public BootstrapSystemUser() {
029: // setLevel(new Integer(PATCH_LEVEL));
030: // setName(PATCH_NAME);
031: }
032:
033: public void run(String sysadminId) throws Exception {
034: try {
035: HibernateHelper.initializeHibernate();
036: Session session = GlobalSessionFactory.get().openSession();
037: try {
038: List people = session.find("from person in class "
039: + Person.class.getName()
040: + " where person.userId = ?", sysadminId,
041: Hibernate.STRING);
042: Iterator personItr = people.iterator();
043: Person sysadmin;
044: if (personItr.hasNext()) {
045: sysadmin = (Person) personItr.next();
046: log.info("using " + sysadminId + " user");
047: } else {
048: sysadmin = createSysAdmin(sysadminId, session);
049: }
050: Role viewerRole = initializeRole(session, "viewer", 1,
051: 8);
052: Role editorRole = initializeRole(session, "editor", 2,
053: 7);
054: Role adminRole = initializeRole(session, "admin", 3, 6);
055: Role sysadminRole = initializeRole(session,
056: SYSADMIN_USER_ID, 4, 5);
057: addRoleAssociation(session, sysadminRole.getId(),
058: sysadmin.getId(), 0);
059: createPermission(session, sysadminRole, "%", "%");
060: createNegativePermission(session, editorRole,
061: "system.project", "create.project");
062: createNegativePermission(session, editorRole,
063: "system.person", "create.person");
064: createNegativePermission(session, adminRole,
065: "system.project", "create.project");
066: createPermission(session, adminRole, "%", "admin%");
067: createPermission(session, editorRole, "%", "create%");
068: createPermission(session, editorRole, "%", "edit%");
069: createPermission(session, editorRole, "%", "integrate%");
070: createPermission(session, editorRole, "%", "delete%");
071: createPermission(session, viewerRole, "%", "read%");
072: session.flush();
073: session.connection().commit();
074: } finally {
075: session.close();
076: }
077: } catch (Exception e) {
078: e.printStackTrace();
079: throw e;
080: }
081: }
082:
083: private Person createSysAdmin(String sysadminId, Session session)
084: throws HibernateException {
085: Person sysadmin;
086: log.info("creating " + sysadminId + " user");
087: sysadmin = new Person();
088: sysadmin.setUserId(sysadminId);
089: sysadmin.setName(sysadminId);
090: sysadmin.setInitials("SYS");
091: sysadmin.setEmail("no@reply.com");
092: sysadmin
093: .setPassword("1tGWp1Bdm02Sw4bD7/o0N2ao405Tf8kjxGBW/A=="); // password=admin
094: sysadmin.setLastUpdateTime(new Date());
095: session.save(sysadmin);
096: return sysadmin;
097: }
098:
099: private void addRoleAssociation(Session session, int roleId,
100: int personId, int projectId) throws HibernateException {
101: session.save(new RoleAssociation(projectId, personId, roleId));
102: }
103:
104: private void createPermission(Session session, Role sysadminRole,
105: String resourceType, String permissionName)
106: throws HibernateException {
107: Permission permission = new Permission(resourceType, 0,
108: sysadminRole.getId(), permissionName);
109: session.save(permission);
110: }
111:
112: private void createNegativePermission(Session session,
113: Role sysadminRole, String resourceType,
114: String permissionName) throws HibernateException {
115: Permission permission = new Permission(resourceType, 0,
116: sysadminRole.getId(), permissionName);
117: permission.setPositive(false);
118: session.save(permission);
119: }
120:
121: private Role initializeRole(Session session, String roleName,
122: int left, int right) throws HibernateException {
123: List roles = session.find("from role in class "
124: + Role.class.getName() + " where role.name = ?",
125: roleName, Hibernate.STRING);
126: Role role;
127: if (roles.size() == 0) {
128: log.info("creating role: " + roleName);
129: role = new Role(roleName);
130: role.setLeft(left);
131: role.setRight(right);
132: session.save(role);
133: } else {
134: role = (Role) roles.get(0);
135: }
136: return role;
137: }
138:
139: public void migrate(MigrationContext context)
140: throws MigrationException {
141: try {
142: run(SYSADMIN_USER_ID);
143: } catch (Exception e) {
144: throw new MigrationException("error during migration", e);
145: } finally {
146: HsqlServer.shutdown();
147: }
148: }
149:
150: public static void main(String[] args) {
151: String sysadminId = SYSADMIN_USER_ID;
152: if (args.length == 1) {
153: sysadminId = args[0];
154: }
155: BootstrapSystemUser action = new BootstrapSystemUser();
156: try {
157: action.run(sysadminId);
158: } catch (Exception e) {
159: e.printStackTrace();
160: } finally {
161: HsqlServer.shutdown();
162: }
163: }
164: }
|