/*
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.ui.util;
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.ui.CarbonUIUtil;
import org.wso2.carbon.ui.internal.CarbonUIServiceComponent;
import org.wso2.carbon.core.commons.stub.loggeduserinfo.LoggedUserInfoAdminStub;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.ServerConstants;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
public class CarbonUIAuthenticationUtil {
public static void onSuccessAdminLogin(HttpServletRequest request, String userName)
throws Exception {
HttpSession session = request.getSession();
session.setAttribute("authenticated", Boolean.parseBoolean("true"));
String tenantDomain = UserCoreUtil.getTenantDomain(
CarbonUIServiceComponent.getRealmService(), userName);
if (tenantDomain != null && tenantDomain.trim().length() > 0) {
session.setAttribute(MultitenantConstants.TENANT_DOMAIN, tenantDomain);
// we will make it an attribute on request as well
if (request.getAttribute(MultitenantConstants.TENANT_DOMAIN) == null) {
request.setAttribute(MultitenantConstants.TENANT_DOMAIN, tenantDomain);
}
}
onSuccessAdminLogin(request, userName, tenantDomain);
}
public static void onSuccessAdminLogin(HttpServletRequest request, String userName,
String domain) throws Exception {
HttpSession session = request.getSession();
session.setAttribute("authenticated", Boolean.parseBoolean("true"));
String tenantDomain = UserCoreUtil.getTenantDomain(
CarbonUIServiceComponent.getRealmService(), userName);
if (tenantDomain != null && tenantDomain.trim().length() > 0) {
session.setAttribute(MultitenantConstants.TENANT_DOMAIN, tenantDomain);
// we will make it an attribute on request as well
if (request.getAttribute(MultitenantConstants.TENANT_DOMAIN) == null) {
request.setAttribute(MultitenantConstants.TENANT_DOMAIN, tenantDomain);
}
} else {
request.getSession().setAttribute(MultitenantConstants.IS_SUPER_TENANT, "true");
}
String tenantAwareUserName = UserCoreUtil.getTenantLessUsername(userName);
session.setAttribute(CarbonConstants.LOGGED_USER, tenantAwareUserName);
session.getServletContext().setAttribute(CarbonConstants.LOGGED_USER, tenantAwareUserName);
setUserInformation(session);
}
private static void setUserInformation(HttpSession session) throws RemoteException {
try {
String backendServerURL = (String) session.getAttribute(CarbonConstants.SERVER_URL);
String cookie = (String) session.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
ServletContext servletContext = session.getServletContext();
ConfigurationContext configContext = (ConfigurationContext) servletContext
.getAttribute(CarbonConstants.CONFIGURATION_CONTEXT);
LoggedUserInfoAdminStub stub = new LoggedUserInfoAdminStub(configContext,
backendServerURL + "LoggedUserInfoAdmin");
ServiceClient client = stub._getServiceClient();
Options options = client.getOptions();
options.setManageSession(true);
options.setProperty(HTTPConstants.COOKIE_STRING, cookie);
org.wso2.carbon.core.commons.stub.loggeduserinfo.LoggedUserInfo userInfo = stub.getUserInfo();
String[] permissionArray = userInfo.getUIPermissionOfUser();
ArrayList<String> list = new ArrayList<String>();
for (String permission : permissionArray) {
list.add(permission);
}
session.setAttribute(ServerConstants.USER_PERMISSIONS, list);
if (userInfo.getPasswordExpiration() != null) {
session.setAttribute(ServerConstants.PASSWORD_EXPIRATION,
userInfo.getPasswordExpiration());
}
} catch (AxisFault e) {
throw e;
} catch (RemoteException e) {
throw e;
} catch (Exception e) {
throw new AxisFault("Exception occured", e);
}
}
}