Sunday, 11 December 2011

Liferay Delete User From OpenSSO

  • Deploy opensso-client-jdk15/opensso-client-jdk14 as per your JDK Version in Tomcat.
  •  
  • Click on Configuration
  • Client SDK is successfully configured.
    AMConfig.properties created at
    C:\WINDOWS\system32\config\systemprofile\OpenSSOClient\_Program Files_Apache Software Foundation_Tomcat 7.0_webapps_opensso-client-jdk15_AMConfig.properties
  • Now Create a Java/Web Project ..Or Create a Portlet (Liferay).
  • Copy AMConfig.properties to your Application.(In Source Folder,not in a Package)
  • Add Lib : openssoclientsdk.jar in Lib folder.
  • Write Java File ......with Below Two Methods..
  •  
  •      
  • /**
         * Delete SSO user
         * @param emailAddress
         * @param password
         * @return
         * @throws Exception
         */
        public static boolean deleteSSOUser(String emailAddress,String password) throws Exception{
          
            /*
             * Add Configuration Property file : SSOConfig.properties
             * opensso.username : Admin User Name
             * opensso.password : Admin User Password
               opensso.username=amadmin
               opensso.password=ssoadmin
             */


            ResourceBundle bundle = ResourceBundle.getBundle("SSOConfig"); // Set Your OpenSSO Credential
            String orgName = "/";     System.out.println("Organization: " + orgName);

            SSOTokenManager manager = SSOTokenManager.getInstance();
          
            // Admin SSO context.
            AuthContext adminlc = getAuthcontext(orgName,bundle.getString("opensso.username"),bundle.getString("opensso.password"));
          
            if (adminlc.getStatus() == AuthContext.Status.SUCCESS) {
              
                // User SSO context @Begin.......
                AuthContext lc = getAuthcontext(orgName,emailAddress,password);
                AMIdentity userIdentity = null;
                if (lc.getStatus() == AuthContext.Status.SUCCESS) {
                    SSOToken token = lc.getSSOToken();
              
                    try{
                        userIdentity = IdUtils.getIdentity(token);
                    }catch (Exception e) {
                        e.printStackTrace();
                    }finally{
                         manager.destroyToken(token);
                    }
                }else{
                     System.out.println("User Authentication Failed ....... ");
                     return false;
                }
              
                if(null != userIdentity)
                { 
                    Set set = new HashSet();
                    set.add(userIdentity);
                    // User SSO context @End......
                    SSOToken token = adminlc.getSSOToken();
                  
                    try{
                        AMIdentityRepository idRepo = new AMIdentityRepository(token, orgName);
                        /*
                         * To Get All User....
                        IdSearchControl control = new IdSearchControl();
                        IdSearchResults idsr = idRepo.searchIdentities(IdType.USER, "*", control);
                        Map map_ =  idsr.getResultAttributes();
                        for(Object object:map_.keySet()){
                            System.out.println("keyset  ::  "+object);
                        }
                        */
                        Set set_ =  idRepo.getAllowedIdOperations(IdType.USER);
                        for(Object object:set_){
                            IdOperation operation = (IdOperation)object;
                            if(operation.equals(IdOperation.DELETE)){
                                //idRepo.deleteIdentities(set);
                                System.out.println("SSO User Deleted...");
                                break;
                            }
                        }
                    }catch (Exception e) {
                        return false;
                    }finally{
                        manager.destroyToken(token);
                    }
                }
            }else {
                 System.out.println("Admin Authentication Failed ....... ");
                 return false;
            }
            return true;
        }
      
        /**
         * Creates AuthContext and submits requirements
         * @param orgName : Name of Organization (basically "/")
         * @param userName :
         * @param pwd :
         * @return object of AuthContext
         * @throws AuthLoginException
         * @throws IOException
         */
        private static AuthContext getAuthcontext(String orgName,String userName,String pwd)
            throws AuthLoginException, IOException
        {
            System.out.println("SSOServlet.getAuthcontext()  orgName  ::  "+orgName);
            System.out.println("SSOServlet.getAuthcontext() userName  ::  "+userName);
            System.out.println("SSOServlet.getAuthcontext()      pwd  ::  "+pwd);
            AuthContext lc = new AuthContext(orgName);
            AuthContext.IndexType indexType = AuthContext.IndexType.MODULE_INSTANCE;
            String indexName = "DataStore";
            lc.login(indexType, indexName);

            Callback[] callback = lc.getRequirements();
          
            for (int i =0 ; i< callback.length ; i++) {
                if (callback[i] instanceof NameCallback) {
                    NameCallback name = (NameCallback) callback[i];
                    //System.out.print(name.getPrompt());
                    name.setName(userName);
                } else if (callback[i] instanceof PasswordCallback) {
                    PasswordCallback pass = (PasswordCallback) callback[i];
                    String password = pwd;
                    pass.setPassword(password.toCharArray());
                }
            }

            lc.submitRequirements(callback);
            return lc;
        }



  •  OpenSSO User Successfully Deleted By Above Operation...

















2 comments:

  1. Gre8 Tutorial . But This is for expert...
    Can i have tutorial for beginner like me .......?

    Good Job you done....

    ReplyDelete
  2. Yes Tech New, you can find out documentation for beginner from Liferay offical site

    Link : http://www.liferay.com/documentation/liferay-portal/6.0/getting-started

    First try Administrator book and then Developer Guide.... and also you buy a very good book "Liferay in Action" to begin Liferay.

    ReplyDelete

Infinidb _CpNoTf_ problem

infinidb table with a varchar column insert string as a '_CpNoTf_' while using Cpimport. The Problem is occured if inserted string ...