001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.test.cms.security;
023:
024: import org.hibernate.Session;
025: import org.hibernate.SessionFactory;
026: import org.hibernate.Transaction;
027: import org.jboss.portal.identity.IdentityContext;
028: import org.jboss.portal.identity.IdentityServiceController;
029: import org.jboss.portal.identity.UserModule;
030: import org.jboss.portal.identity.db.HibernateRoleImpl;
031: import org.jboss.portal.identity.db.HibernateUserImpl;
032:
033: import javax.naming.InitialContext;
034:
035: /** @author <a href="mailto:sshah@redhat.com">Sohil Shah</a> */
036: public class IdentityDataLoader {
037: /**
038: *
039: */
040: private IdentityServiceController identityServiceController = null;
041:
042: /**
043: *
044: */
045: private String identitySessionFactory = null;
046:
047: /** @return */
048: public IdentityServiceController getIdentityServiceController() {
049: return this .identityServiceController;
050: }
051:
052: public void setIdentityServiceController(
053: IdentityServiceController identityServiceController) {
054: this .identityServiceController = identityServiceController;
055: }
056:
057: /** @return */
058: public String getIdentitySessionFactory() {
059: return identitySessionFactory;
060: }
061:
062: /** @param identitySessionFactory */
063: public void setIdentitySessionFactory(String identitySessionFactory) {
064: this .identitySessionFactory = identitySessionFactory;
065: }
066:
067: /**
068: *
069: *
070: */
071: public IdentityDataLoader() {
072:
073: }
074:
075: /**
076: *
077: *
078: */
079: public void start() throws Exception {
080: SessionFactory sessionFactory = (SessionFactory) new InitialContext()
081: .lookup(this .identitySessionFactory);
082: this .loadData(sessionFactory, this .identityServiceController);
083: }
084:
085: /**
086: *
087: */
088: public void stop() throws Exception {
089: }
090:
091: /**
092: *
093: *
094: */
095: private void loadData(SessionFactory sessionFactory,
096: IdentityServiceController service) throws Exception {
097: UserModule userModule = (UserModule) service
098: .getIdentityContext().getObject(
099: IdentityContext.TYPE_USER_MODULE);
100:
101: Session session = sessionFactory.openSession();
102: Transaction tx = session.beginTransaction();
103: try {
104: if (userModule.getUserCount() == 0) {
105: //
106: HibernateRoleImpl adminRole = new HibernateRoleImpl(
107: "Admin", "Administrators");
108: HibernateRoleImpl userRole = new HibernateRoleImpl(
109: "User", "Users");
110:
111: //
112: HibernateUserImpl admin = new HibernateUserImpl("admin");
113: admin.setPassword(org.jboss.portal.common.util.Tools
114: .md5AsHexString("admin"));
115: admin.setRealEmail("admin@portal.com");
116: admin.setViewRealEmail(true);
117: admin.setEnabled(true);
118:
119: //
120: admin.getRoles().add(adminRole);
121: adminRole.getUsers().add(admin);
122:
123: //
124: HibernateUserImpl user = new HibernateUserImpl("user");
125: user.setPassword(org.jboss.portal.common.util.Tools
126: .md5AsHexString("user"));
127: user.setRealEmail("user@portal.com");
128: user.setViewRealEmail(true);
129: user.setEnabled(true);
130:
131: //
132: user.getRoles().add(userRole);
133: userRole.getUsers().add(user);
134:
135: //Another admin user besides the core admin user
136: HibernateUserImpl sysAdmin = new HibernateUserImpl(
137: "sysadmin");
138: sysAdmin.setPassword(org.jboss.portal.common.util.Tools
139: .md5AsHexString("sysadmin"));
140: sysAdmin.setRealEmail("sysadmin@portal.com");
141: sysAdmin.setViewRealEmail(true);
142: sysAdmin.setEnabled(true);
143:
144: //
145: //
146: sysAdmin.getRoles().add(adminRole);
147: adminRole.getUsers().add(sysAdmin);
148:
149: //
150: session.save(adminRole);
151: session.save(userRole);
152: session.save(admin);
153: session.save(user);
154: session.save(sysAdmin);
155:
156: tx.commit();
157: }
158: } catch (Exception e) {
159: tx.rollback();
160: } finally {
161: if (session != null) {
162: session.close();
163: }
164: }
165: }
166: }
|