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> GetFunction { get; set; } public Func CheckFunction { get; set; } } private readonly Dictionary _funcs = new Dictionary(); 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 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 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 GetIdentities(ProcessInstance processInstance, WorkflowRuntime runtime, string ruleName, string parameter) { return !_funcs.ContainsKey(ruleName) ? new List { } : _funcs[ruleName].GetFunction.Invoke(processInstance, parameter); } } }