Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Created June 7, 2013 03:33
Show Gist options
  • Select an option

  • Save gastaldi/5726909 to your computer and use it in GitHub Desktop.

Select an option

Save gastaldi/5726909 to your computer and use it in GitHub Desktop.
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.example.multi.picketlink.idm;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import org.picketlink.idm.IdentityManager;
import org.picketlink.idm.credential.Password;
import org.picketlink.idm.internal.IdentityManagerFactory;
import org.picketlink.idm.model.Attribute;
import org.picketlink.idm.model.Group;
import org.picketlink.idm.model.Realm;
import org.picketlink.idm.model.Role;
import org.picketlink.idm.model.SimpleGroup;
import org.picketlink.idm.model.SimpleRole;
import org.picketlink.idm.model.SimpleUser;
import org.picketlink.idm.model.User;
@Singleton
@Startup
public class IDMInitializer
{
@Inject
IdentityManagerFactory factory;
// @Inject
// private IdentityManager identityManager;
@PostConstruct
public void create()
{
Realm realmOne = new Realm("businessOne");
Realm realmTwo = new Realm("businessTwo");
IdentityManager idManagerOne = factory.createIdentityManager(realmOne);
IdentityManager idManagerTwo = factory.createIdentityManager(realmTwo);
Attribute<String> businessOne = new Attribute<String>("corporate", "businessOne");
Attribute<String> businessTwo = new Attribute<String>("corporate", "businessTwo");
// Create user john
User john = new SimpleUser("john");
john.setEmail("john@acme.com");
john.setFirstName("John");
john.setLastName("Smith");
john.setAttribute(businessOne);
john.setPartition(realmOne);
idManagerOne.add(john);
idManagerOne.updateCredential(john, new Password("demo"));
// Create user mary
User mary = new SimpleUser("mary");
mary.setEmail("mary@acme.com");
mary.setFirstName("Mary");
mary.setLastName("Jones");
mary.setAttribute(businessOne);
mary.setPartition(realmOne);
idManagerOne.add(mary);
idManagerOne.updateCredential(mary, new Password("demo"));
// Create user jane
User jane = new SimpleUser("jane");
jane.setEmail("jane@acme.com");
jane.setFirstName("Jane");
jane.setLastName("Doe");
jane.setAttribute(businessTwo);
jane.setPartition(realmTwo);
idManagerTwo.add(jane);
idManagerTwo.updateCredential(jane, new Password("demo"));
// Create role "manager"
Role manager = new SimpleRole("manager");
idManagerOne.add(manager);
idManagerTwo.add(manager);
// Create application role "superuser"
Role superuser = new SimpleRole("superuser");
idManagerOne.add(superuser);
idManagerTwo.add(superuser);
// Create group "sales"
Group sales = new SimpleGroup("sales");
idManagerOne.add(sales);
idManagerTwo.add(sales);
// Make john a member of the "sales" group
idManagerTwo.addToGroup(john, sales);
// Make mary a manager of the "sales" group
idManagerTwo.grantGroupRole(mary, manager, sales);
// Grant the "superuser" application role to jane
idManagerTwo.grantRole(jane, superuser);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment