/// <summary> /// Used to get the Permission for Role Object /// </summary> /// <param name="roleId"></param> /// <param name="objectId"></param> /// <returns></returns> public static Permission GetRoleObjectPermission(int roleId, int objectId = 0, int moduleId = 0, int attributeId = 0) { try { if (!_tokenManager.GenerateToken("admin", "admin")) { throw new ArgumentException("Unable to Authorize the application"); } if (objectId == 0 && attributeId != 0) { _ISpartan_MetadataApiConsumer.SetAuthHeader(_tokenManager.Token); var resultMeta = _ISpartan_MetadataApiConsumer.GetByKey(attributeId, false).Resource; objectId = Convert.ToInt32(resultMeta.Related_Object_Id); moduleId = 0; } _spartaneUserRoleObjectFunctionApiConsumer.SetAuthHeader(_tokenManager.Token); _spartaneFunctionApiConsumer.SetAuthHeader(_tokenManager.Token); string where = "spartan_user_rule_object_function.Object_Id=" + objectId + " AND spartan_user_rule_object_function.Spartan_User_Rule=" + roleId; if (moduleId != 0) { where += " AND spartan_user_rule_object_function.Module_Id=" + moduleId; } var userRoleObjectFunctions = _spartaneUserRoleObjectFunctionApiConsumer.ListaSelAll(1, int.MaxValue, where, "").Resource; if (userRoleObjectFunctions == null || userRoleObjectFunctions.Spartan_User_Rule_Object_Functions == null) { return(new Permission()); } var spartaneFuctions = new List <SpartaneFunction>(); foreach (var userRoleObjectFunction in userRoleObjectFunctions.Spartan_User_Rule_Object_Functions) { spartaneFuctions.Add(_spartaneFunctionApiConsumer.GetByKey(userRoleObjectFunction.Fuction_Id, true).Resource); } Permission ObjectPermissions = GetSpartanePermission(spartaneFuctions); //WorkFlows Permissions #region WorkFlows Permissions if (System.Web.HttpContext.Current.Session != null) { if (System.Web.HttpContext.Current.Session["Phase"] != null) { if (System.Web.HttpContext.Current.Session["Phase"].ToString() != "") { //Consult if the object has workflow assigned _Spartan_WorkFlowApiConsumer.SetAuthHeader(_tokenManager.Token); var workflowObject = _Spartan_WorkFlowApiConsumer.ListaSelAll(1, int.MaxValue, "spartan_workflow.object = " + objectId.ToString() + " and spartan_workflow.status=1", "").Resource; if (workflowObject != null) { if (workflowObject.Spartan_WorkFlows.Count != 0) { //Consultar id de Fase _Spartan_WorkFlow_PhasesApiConsumer.SetAuthHeader(_tokenManager.Token); var phaseObject = _Spartan_WorkFlow_PhasesApiConsumer.ListaSelAll(1, int.MaxValue, "Spartan_WorkFlow_Phases.WorkFlow=" + workflowObject.Spartan_WorkFlows[0].WorkFlowId.ToString() + " and Spartan_WorkFlow_Phases.Phase_Number=" + System.Web.HttpContext.Current.Session["Phase"].ToString() + " and Spartan_WorkFlow_Phases.phase_status=1", "").Resource; if (phaseObject != null) { if (phaseObject.Spartan_WorkFlow_Phasess.Count != 0) { //Consultar Permisos _Spartan_WorkFlow_Roles_by_StateApiConsumer.SetAuthHeader(_tokenManager.Token); var rolesObject = _Spartan_WorkFlow_Roles_by_StateApiConsumer.ListaSelAll(1, int.MaxValue, "Spartan_WorkFlow_Roles_by_State.Spartan_WorkFlow=" + workflowObject.Spartan_WorkFlows[0].WorkFlowId.ToString() + " and Spartan_WorkFlow_Roles_by_State.Phase=" + phaseObject.Spartan_WorkFlow_Phasess[0].PhasesId.ToString() + " and Spartan_WorkFlow_Roles_by_State.User_Role=" + roleId.ToString(), "").Resource; if (rolesObject != null) { if (rolesObject.Spartan_WorkFlow_Roles_by_States.Count != 0) { if (rolesObject.Spartan_WorkFlow_Roles_by_States[0].Permission_To_New == false) { ObjectPermissions.New = false; } if (rolesObject.Spartan_WorkFlow_Roles_by_States[0].Permission_To_Modify == false) { ObjectPermissions.Edit = false; } if (rolesObject.Spartan_WorkFlow_Roles_by_States[0].Permission_to_Delete == false) { ObjectPermissions.Delete = false; } if (rolesObject.Spartan_WorkFlow_Roles_by_States[0].Permission_To_Export == false) { ObjectPermissions.Export = false; } if (rolesObject.Spartan_WorkFlow_Roles_by_States[0].Permission_To_Print == false) { ObjectPermissions.Print = false; } if (rolesObject.Spartan_WorkFlow_Roles_by_States[0].Permission_Settings == false) { ObjectPermissions.Configure = false; } } } } } } } } } } #endregion return(ObjectPermissions); } catch (ArgumentException) { return(new Permission()); } catch (Exception ex) { throw ex; } }
//CAMBIOS PERMISOS public static void SetPermissions() { List <Permission> permissions = new List <Permission>(); if (!_tokenManager.GenerateToken("admin", "admin")) { throw new ArgumentException("Unable to Authorize the application"); } _spartaneUserRoleObjectFunctionApiConsumer.SetAuthHeader(_tokenManager.Token); _spartaneFunctionApiConsumer.SetAuthHeader(_tokenManager.Token); var userRoleObjectFunctions = _spartaneUserRoleObjectFunctionApiConsumer.ListaSelAll(1, int.MaxValue, "", "").Resource; if (userRoleObjectFunctions == null || userRoleObjectFunctions.Spartan_User_Rule_Object_Functions == null) { System.Web.HttpContext.Current.Session["Permissions"] = null; } var spartaneFuctions = new List <SpartaneFunction>(); Permission ObjectPermission = null; bool exist = true; foreach (var userRoleObjectFunction in userRoleObjectFunctions.Spartan_User_Rule_Object_Functions) { exist = true; ObjectPermission = permissions.Where(x => x.Module == userRoleObjectFunction.Module_Id && x.Role == userRoleObjectFunction.Spartan_User_Rule && x.Object == userRoleObjectFunction.Object_Id).FirstOrDefault(); if (ObjectPermission == null) { ObjectPermission = new Permission(); ObjectPermission.New = false; ObjectPermission.Consult = false; ObjectPermission.Edit = false; ObjectPermission.Delete = false; ObjectPermission.Export = false; ObjectPermission.Print = false; ObjectPermission.Configure = false; exist = false; } ObjectPermission.Module = userRoleObjectFunction.Module_Id; ObjectPermission.Role = userRoleObjectFunction.Spartan_User_Rule; ObjectPermission.Object = userRoleObjectFunction.Object_Id; switch (userRoleObjectFunction.Fuction_Id) { case 1: ObjectPermission.Consult = true; break; case 2: ObjectPermission.New = true; break; case 3: ObjectPermission.Edit = true; break; case 4: ObjectPermission.Delete = true; break; case 5: ObjectPermission.Export = true; break; case 6: ObjectPermission.Print = true; break; case 7: ObjectPermission.Configure = true; break; } if (!exist) { permissions.Add(ObjectPermission); } } System.Web.HttpContext.Current.Session["Permissions"] = permissions; }