Skip to Content
avatar image
Former Member

Does anyone know how to create users and add roles programatically?

Hi, everyone I was looking for create user and add them roles dinamically but I coudn't find something, my problem is that I have a trial version Portal and I don't if this had a problem to use portal apis,

thank you

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 16, 2008 at 08:00 AM

    Hi Israel,

    For creating a portal user through webdynpro follow the steps mentioned in the below article.

    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/f0bd5112-4456-2b10-c28b-9654d5ae8111

    You use the IUser API to create the users. Similarly there is one IRole API. Using this You can access the existing roles, create new roles as well as assign or remove users from existing roles.

    The following is the code for the same:

    import com.sap.security.api.*;

    // assign individual user to a Role

    String userid = "<unique-id of user>";

    String roleid = "<unique-id of role>";

    IRoleFactory roleFact = UMFactory.getRoleFactory();

    roleFact.addUserToRole(userid, roleid);

    Hope this answers your query.

    Regards,

    Anagha

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 16, 2008 at 05:06 AM

    hi,

    i have done role assign to some users dynamically.

    what is your requrement let me know i will give you code for that.

    jayesh talreja

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi jayesh talreja, my requierement is that I need to add roles dinamycally to diferents users, my problem is that I want to use only one portal user and depending of the user logged assign this portal user some roles previously created, in other word is possible that many users use the same portal user with diferent roles?

      thanks

  • avatar image
    Former Member
    Oct 16, 2008 at 08:00 AM

    Hi,

    Here is the complete code for your requirement

    Create a new user

    import com.sap.security.api.IGroupFactory;

    import com.sap.security.api.lFactory;

    import com.sap.security.api.IUserAccount;

    import com.sap.security.api.IUserFactory;

    import com.sap.security.api.IUserMaint;

    import com.sap.security.api.IUserSearchFilter;

    import com.sap.security.api.UMException;

    import com.sap.security.api.UMFactory;

    public static void createUser throws UMException {

    IRoleFactory roleFact = UMFactory.getRoleFactory();

    IGroupFactory groupFact = UMFactory.getGroupFactory();

    IUserFactory userFact = UMFactory.getUserFactory();

    IUserMaint epUser = null;

    IUserAccount userAcc = null;

    try {

    epUser = userFact.newUser( u201Csanthoshu201D );

    epUser.setFirstName( u201CSanthoshu201D );

    epUser.setLastName( u201CRaju201D );

    epUser.save();

    epUser.commit();

    try {

    userAcc = UMFactory.getUserAccountFactory().newUserAccount( u201Cmiltongu201D,

    epUser.getUniqueID());

    userAcc.setPassword( u201Csap@123u201D );

    userAcc.save();

    userAcc.commit();

    } catch (Exception e)

    {

    System.out.println(e.toString());

    return;

    }

    } catch (Exception exp)

    {

    System.out.println(exp.toString());

    epUser.rollback();

    }

    }

    Assign Role to User

    Consider an already created role named epRole.

    String uniqueIdOfUser = epUser.getUniqueID();

    String uniqueIdOfRole= UMFactory.getRoleFactory().getRoleByUniqueName("pcd:portal_content/com.sap.customRoles/epRole").getUniqueID());

    UMFactory.getRoleFactory().addUserToRole( uniqueIdOfUser, uniqueIdOfRole);

    Dont forget to import the com.sap.security.api.

    Regards,

    Santhosh

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 15, 2008 at 08:49 PM

    HI

    You must make use of UME API

    IUserMaint user = UMFactory.getUserFactory().newUser("AAA");

    user.setFirstName("BBB");

    user.setLastName("CCC");

    user.save();

    user.commit();

    //Assigning Group

    String uniqueIdOfGroup = UMFactory.getGroupFactory().getGroupByUniqueName("GROUP_NAME").getUniqueID();

    UMFactory.getGroupFactory().addUserToGroup(user.getUniqueID(), uniqueIdOfGroup);

    IUserAccount uacc = UMFactory.getUserAccountFactory().newUserAccount("AAA", user.getUniqueID());

    uacc.setPassword("pass1");

    uacc.setPasswordChangeRequired(false);

    uacc.save();

    uacc.commit();

    Hope it helps

    Donate points for useful answers

    MMK

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Your answer was very hopeful could you send me a example about how to add a role to user in same way like the other example, thanks

  • avatar image
    Former Member
    Oct 27, 2008 at 04:18 PM

    thanks for your help it was very useful

    Add comment
    10|10000 characters needed characters exceeded