/**
* Copyright (c) 2014 Evolveum
*
* 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 com.evolveum.midpoint.model.common.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.evolveum.midpoint.audit.api.AuditEventRecord;
import com.evolveum.midpoint.audit.api.AuditEventStage;
import com.evolveum.midpoint.audit.api.AuditEventType;
import com.evolveum.midpoint.audit.api.AuditService;
import com.evolveum.midpoint.model.api.ModelService;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
/**
* Abstract superclass that provides methods common to all web service implementations that
* use ModelService.
*
* @author Radovan Semancik
*
*/
public abstract class AbstractModelWebService {
@Autowired(required = true)
protected ModelService modelService;
@Autowired(required = true)
protected TaskManager taskManager;
@Autowired(required = true)
protected AuditService auditService;
@Autowired(required = true)
protected PrismContext prismContext;
protected void setTaskOwner(Task task) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new SystemException("Failed to get authentication object");
}
UserType userType = (UserType) ((MidPointPrincipal)(SecurityContextHolder.getContext().getAuthentication().getPrincipal())).getUser();
if (userType == null) {
throw new SystemException("Failed to get user from authentication object");
}
task.setOwner(userType.asPrismObject());
}
protected Task createTaskInstance(String operationName) {
// TODO: better task initialization
Task task = taskManager.createTaskInstance(operationName);
setTaskOwner(task);
task.setChannel(SchemaConstants.CHANNEL_WEB_SERVICE_URI);
return task;
}
protected void auditLogin(Task task) {
AuditEventRecord record = new AuditEventRecord(AuditEventType.CREATE_SESSION, AuditEventStage.REQUEST);
PrismObject<UserType> owner = task.getOwner();
if (owner != null) {
record.setInitiator(owner);
PolyStringType name = owner.asObjectable().getName();
if (name != null) {
record.setParameter(name.getOrig());
}
}
record.setChannel(SchemaConstants.CHANNEL_WEB_SERVICE_URI);
record.setTimestamp(System.currentTimeMillis());
record.setSessionIdentifier(task.getTaskIdentifier());
record.setOutcome(OperationResultStatus.SUCCESS);
auditService.audit(record, task);
}
protected void auditLogout(Task task) {
AuditEventRecord record = new AuditEventRecord(AuditEventType.TERMINATE_SESSION, AuditEventStage.REQUEST);
PrismObject<UserType> owner = task.getOwner();
if (owner != null) {
record.setInitiator(owner);
PolyStringType name = owner.asObjectable().getName();
if (name != null) {
record.setParameter(name.getOrig());
}
}
record.setChannel(SchemaConstants.CHANNEL_WEB_SERVICE_URI);
record.setTimestamp(System.currentTimeMillis());
record.setSessionIdentifier(task.getTaskIdentifier());
record.setOutcome(OperationResultStatus.SUCCESS);
auditService.audit(record, task);
}
}