AnsweredAssumed Answered

how can i set admin role for new user

Question asked by arun_sira on May 30, 2007
Latest reply on Jun 13, 2007 by janvg
Hi all,

i have created new user as guest in alfresco from my code. But i cont able to create user with admin Role, can you please give coding to create user with admin functionality

My mail id: arun_sira@hotmail.com

My Code:







<%


String username ="newuser1";
String password ="newuser1";
String oldusername ="admin";
String oldpassword="admin";
ArrayList datalist=new ArrayList();


username = request.getParameter("username");
password = request.getParameter("password");


// form.setPassword(password);
// form.setConfirm(password);

NewUserWizard newuserobject=new NewUserWizard();

/** AuthenticationService bean reference */
AuthenticationService authenticationService = null;
AuthenticationService authenticationServicenew = null;

/** NodeService bean reference */
NodeService nodeService = null;

FacesContext context = FacesContext.getCurrentInstance();
nodeService = Repository.getServiceRegistry(context).getNodeService();


WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(application);
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);

// get individual, required services


UserTransaction tx ;
//nodeService = (NodeService)serviceRegistry.getNodeService();
tx = serviceRegistry.getTransactionService().getUserTransaction();
tx.begin();

authenticationService=(AuthenticationService)ctx.getBean("authenticationService");
PersonService personService = (PersonService)ctx.getBean("personService");



try {

//logout first
//FacesContext context = FacesContext.getCurrentInstance();

// invalidate User ticket
User user = (User)session.getValue(AuthenticationHelper.AUTHENTICATION_USER);
if (user != null)
{
authenticationService.invalidateTicket(user.getTicket());

Enumeration em= session.getAttributeNames();
while(em.hasMoreElements()) {
String name=(String)em.nextElement();
session.removeAttribute(name);
}

}


authenticationService.authenticate(oldusername,oldpassword.toCharArray());

// setup User object and Home space ID

user = new User(authenticationService.getCurrentUserName(),authenticationService.getCurrentTicket(),personService.getPerson(oldusername));

String homeSpaceId = null;


//nothing but workspace guid


homeSpaceId ="de61ef7a-0ab9-11dc-a5ea-7b72135c349f";




// set the current user workspace

user.setHomeSpaceId(homeSpaceId);



//inintialize the authentication session

session.putValue(AuthenticationHelper.AUTHENTICATION_USER, user);

//get the noderep object form Noderef bean

NodeRef homeSpaceRef = new NodeRef(Repository.getStoreRef(), homeSpaceId);

String homeSpaceName = Repository.getNameForNode((NodeService)serviceRegistry.getNodeService(), homeSpaceRef);

authenticationServicenew=(AuthenticationService)ctx.getBean("authenticationService");

authenticationServicenew.createAuthentication(username,password.toCharArray());
System.out.println("after creating user " );





PermissionService permissionService = (PermissionService) ctx.getBean("permissionService");
permissionService.setPermission(homeSpaceRef, permissionService.getAllAuthorities(),PermissionService.ALL_PERMISSIONS, true);




authenticationService.authenticate(username, password.toCharArray());

// setup User object and Home space ID
user = new User(authenticationService.getCurrentUserName(), authenticationService.getCurrentTicket(),personService.getPerson(username));

homeSpaceId = null;


//nothing but workspace guid ,person



homeSpaceId ="de61ef7a-0ab9-11dc-a5ea-7b72135c349f";




// set the current user workspace

user.setHomeSpaceId(homeSpaceId);



//inintialize the authentication session

session.putValue(AuthenticationHelper.AUTHENTICATION_USER, user);

//get the noderep object form Noderef bean

NodeRef homeSpaceRef1 = new NodeRef(Repository.getStoreRef(), homeSpaceId);
homeSpaceName = Repository.getNameForNode((NodeService)serviceRegistry.getNodeService(), homeSpaceRef1);
// PermissionService.inheritPermissions(homeSpaceRef,homeSpaceRef1);

response.sendRedirect(request.getContextPath() +"/auto.jsp?eu="+username+"&dp="+password);
} catch (Exception ex) {
// System.out.println("something is error." + ex.getMessage());
//response.sendRedirect( request.getContextPath() +"OpenSesami/Dashboard.jsp");
out.println("something is error." + ex.getMessage());
// username="admin";
// password="admin";
// response.sendRedirect(request.getContextPath() +"/auto.jsp?username="+username+"&password="+password);
// response.sendRedirect(request.getContextPath() +"/auto.jsp?eu="+username+"&dp="+password);
//ex.printStackTrace();
}


%>

Outcomes