97 lines
4.2 KiB
C#
97 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OptimaJet.Workflow.Core.Runtime;
|
|
using OptimaJet.Workflow.Core.Model;
|
|
using EnVisage;
|
|
using EnVisage.Code;
|
|
|
|
namespace EnVisage.Code.WorkFlow
|
|
{
|
|
public class WorkflowRule : IWorkflowRuleProvider
|
|
{
|
|
private class RuleFunction
|
|
{
|
|
public Func<ProcessInstance, string, IEnumerable<string>> GetFunction { get; set; }
|
|
|
|
public Func<ProcessInstance, string, string, bool> CheckFunction { get; set; }
|
|
}
|
|
|
|
private readonly Dictionary<string, RuleFunction> _funcs =
|
|
new Dictionary<string, RuleFunction>();
|
|
|
|
|
|
public WorkflowRule()
|
|
{
|
|
_funcs.Add("CheckRole", new RuleFunction() { CheckFunction = isUserInRole, GetFunction = GetRole });
|
|
_funcs.Add("CheckRoleApprovals", new RuleFunction() { CheckFunction = isUserInRoleWithPermission, GetFunction = GetRole });
|
|
_funcs.Add("CheckRoleContact", new RuleFunction() { CheckFunction = isUserInRoleAndContact, GetFunction = GetRole });
|
|
|
|
}
|
|
private IEnumerable<string> GetRole(ProcessInstance processInstance, string parameter)
|
|
{
|
|
using (var context = new EnVisageEntities())
|
|
{
|
|
return
|
|
context.User2WorkFlowRole.Where(r => r.WorkFlowRole.RoleName == parameter).ToList()
|
|
.Select(r => r.UserId.ToString()).ToList();
|
|
|
|
}
|
|
}
|
|
private bool isUserInRole(ProcessInstance processInstance, string identityId, string parameter)
|
|
{
|
|
using (var context = new EnVisageEntities())
|
|
{
|
|
bool ok= (context.AspNetRoles.Any(x => x.AspNetUsers.Any(y => y.Id == identityId) && x.Name == parameter));
|
|
if (ok)
|
|
return ok;
|
|
ok= (context.User2WorkFlowRole.Any(x => x.Id != Guid.Empty && x.AspNetUser.Id == identityId && x.WorkFlowRole.RoleName == parameter));
|
|
return ok;
|
|
}
|
|
}
|
|
private bool isUserInRoleWithPermission(ProcessInstance processInstance, string identityId, string parameter)
|
|
{
|
|
using (var context = new EnVisageEntities())
|
|
{
|
|
var userid = Guid.Parse(identityId);
|
|
return context.WorkFlowParallelApprovals.Any(x => x.ProcessId == processInstance.ProcessId && x.UserId == userid && x.IsActive == true && x.Approved ==false);
|
|
}
|
|
}
|
|
private bool isUserInRoleAndContact(ProcessInstance processInstance, string identityId, string parameter)
|
|
{
|
|
using (var context = new EnVisageEntities())
|
|
{
|
|
var userid = Guid.Parse(identityId);
|
|
var projectId = context.Scenarios.Where(x => x.Id == processInstance.ProcessId).Select(x => x.ParentId).FirstOrDefault();
|
|
if (projectId == null || projectId == Guid.Empty)
|
|
return false;
|
|
var email = context.AspNetUsers.Where(x => x.Id == identityId).Select(x => x.Email).FirstOrDefault();
|
|
if (string.IsNullOrEmpty(email))
|
|
return false;
|
|
var contactId = context.Contacts.Where(x => x.Email == email).Select(x => x.Id).FirstOrDefault();
|
|
if (contactId == null || contactId == Guid.Empty)
|
|
return false;
|
|
return context.Contact2Project.Any(x => x.ContactId == contactId && x.Project.Id == projectId);
|
|
}
|
|
}
|
|
public List<string> GetRules()
|
|
{
|
|
return _funcs.Select(c => c.Key).ToList();
|
|
|
|
}
|
|
|
|
|
|
public bool Check(ProcessInstance processInstance, WorkflowRuntime runtime, string identityId, string ruleName, string parameter)
|
|
{
|
|
return _funcs.ContainsKey(ruleName) && _funcs[ruleName].CheckFunction.Invoke(processInstance, identityId, parameter);
|
|
}
|
|
|
|
public IEnumerable<string> GetIdentities(ProcessInstance processInstance, WorkflowRuntime runtime, string ruleName, string parameter)
|
|
{
|
|
return !_funcs.ContainsKey(ruleName)
|
|
? new List<string> { }
|
|
: _funcs[ruleName].GetFunction.Invoke(processInstance, parameter);
|
|
}
|
|
}
|
|
}
|