/*
* Copyright (c) 2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed 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.admin.mgt.util;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.multitenancy.SuperTenantCarbonContext;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.ResourceImpl;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.utils.CarbonUtils;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
public class Util {
private static final Log log = LogFactory.getLog(Util.class);
private static RegistryService registryService;
public static Map<String, AdminManagementConfig> serviceConfigMap =
new HashMap<String, AdminManagementConfig>();
private static AdminManagementConfig adminMgtConfig;
public static synchronized void setRegistryService(RegistryService service) {
if (registryService == null) {
registryService = service;
}
}
public static RegistryService getRegistryService() {
return registryService;
}
public static UserRegistry getConfigSystemRegistry(int tenantId) throws RegistryException {
return registryService.getConfigSystemRegistry(tenantId);
}
/**
* method to load the adminManagementConfig
*/
public static void loadAdminManagementConfig() {
String confXml = CarbonUtils.getCarbonConfigDirPath() + "/email-admin-config.xml";
adminMgtConfig = loadAdminManagementConfig(confXml);
}
/**
* Loading the AdminManagementConfig details from the given config file,
*
* @param configFilename - configuration file
* @return - admin management config
*/
public static AdminManagementConfig loadAdminManagementConfig(String configFilename) {
AdminManagementConfig config = new AdminManagementConfig();
File configfile = new File(configFilename);
if (!configfile.exists()) {
log.error("Email Configuration File is not present at: " + configFilename);
return null;
}
try {
XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(
new FileInputStream(configfile));
StAXOMBuilder builder = new StAXOMBuilder(parser);
OMElement documentElement = builder.getDocumentElement();
Iterator it = documentElement.getChildElements();
while (it.hasNext()) {
OMElement element = (OMElement) it.next();
if ("subject".equals(element.getLocalName())) {
config.setSubject(element.getText());
} else if ("body".equals(element.getLocalName())) {
config.setEmailBody(element.getText());
} else if ("footer".equals(element.getLocalName())) {
config.setEmailFooter(element.getText());
} else if ("targetEpr".equals(element.getLocalName())) {
config.setTargetEpr(element.getText());
} else if ("redirectPath".equals(element.getLocalName())) {
config.setRedirectPath(element.getText());
}
}
return config;
} catch (Exception e) {
String msg = "Error in loading configuration for configuring the admin user: " +
configFilename + ".";
log.error(msg, e);
return null;
}
}
/**
* Confirm that the admin management request has been sent by the user.
*
* @param secretKey the secret key to be sent
* @return ConfirmationBean
* @throws Exception if admin account management attempt failed.
*/
public static ConfirmationBean confirmUser(String secretKey) throws Exception {
ConfirmationBean confirmationBean = new ConfirmationBean();
OMFactory fac = OMAbstractFactory.getOMFactory();
OMElement data = fac.createOMElement("configuration", null);
Registry registry = Util.getConfigSystemRegistry(0);
boolean success = false;
try {
registry.beginTransaction();
String secretKeyPath = AdminMgtConstants.ADMIN_MANAGEMENT_COLLECTION + "/" + secretKey;
if (!registry.resourceExists(secretKeyPath)) {
String msg = "Failed Admin account management attempt.";
log.error(msg);
throw new Exception(msg);
}
Resource resource = registry.get(secretKeyPath);
// just get the properties of that
Properties props = resource.getProperties();
for (Object o : props.keySet()) {
String key = (String) o;
OMElement internal = fac.createOMElement(key, null);
internal.setText(resource.getProperty(key));
data.addChild(internal);
if (key.equals("redirectPath")) {
confirmationBean.setRedirectPath(resource.getProperty(key));
}
}
// removing the temporarily stored data from the registry
registry.delete(resource.getPath());
confirmationBean.setData(data.toString());
success = true;
} finally {
if (success) {
registry.commitTransaction();
} else {
registry.rollbackTransaction();
}
}
return confirmationBean;
}
/**
* verifying the admin management request from the user
*
* @param data - data to include in the mail
* @throws Exception if loading config or sending verification fail.
*/
public static void requestUserVerification(Map<String, String> data) throws Exception {
try {
loadAdminManagementConfig();
} catch (Exception e) {
String msg = "Error in loading the admin management configurations";
log.error(msg, e);
throw new Exception(msg, e);
}
try {
requestUserVerification(data, adminMgtConfig);
} catch (Exception e) {
String msg = "Error in sending verification";
log.error(msg, e);
throw new Exception(msg, e);
}
}
/**
* verifying the admin management request
*
* @param data - data to include in the mail
* @param serviceConfig - adminManagementConfig
* @throws Exception if sending verification fails.
*/
public static void requestUserVerification(
Map<String, String> data, AdminManagementConfig serviceConfig) throws Exception {
String emailAddress = data.get("email");
Map<String, String> userParams = new HashMap<String, String>();
userParams.put("admin-name", data.get("admin"));
userParams.put("domain-name", data.get("tenantDomain"));
userParams.put("first-name", data.get("first-name"));
emailAddress = emailAddress.trim();
try {
String secretKey = UUID.randomUUID().toString();
// The password reset request hasn't been verified by the tenant yet.
// Hence using the super tenant registry instance
Registry registry = Util.getConfigSystemRegistry(0);
Resource resource = registry.newResource();
// store the redirector url
resource.setProperty("redirectPath", serviceConfig.getRedirectPath());
// store the user data, redirectPath can be overwritten here.
for (String s : data.keySet()) {
resource.setProperty(s, data.get(s));
}
((ResourceImpl) resource).setVersionableChange(false);
String secretKeyPath = AdminMgtConstants.ADMIN_MANAGEMENT_COLLECTION + "/" + secretKey;
registry.put(secretKeyPath, resource);
// sending the mail
EmailSender sender = new EmailSender(serviceConfig, emailAddress, secretKey,
SuperTenantCarbonContext.getCurrentContext().getTenantDomain(true), userParams);
sender.sendEmail();
} catch (Exception e) {
String msg = "Error in sending the email.";
log.error(msg, e);
throw new Exception(msg, e);
}
}
}