public ActionResult Save(EditNotificationModel model) { using (NotificationManager nManager = new NotificationManager()) using (EntityPermissionManager pManager = new EntityPermissionManager()) using (EntityManager entityTypeManager = new EntityManager()) using (UserManager userManager = new UserManager()) { Dictionary <long, List <string> > dictionary = (Dictionary <long, List <string> >)Session["ResourceFilter"]; if (ModelState.IsValid && dictionary != null) { //if edit need comarison between stored dependensies and set in session //get resource filter from the notification //Dictionary<long, List<string>> dictionary = (Dictionary<long, List<string>>)Session["ResourceFilter"]; Session["ResourceFilter"] = null; List <Schedule> affectedSchedules = GetAffectedSchedules(dictionary, model.StartDate, model.EndDate); Notification notification = new Rbm.Entities.Booking.Notification(); if (model.Id == 0) { notification = nManager.CreateNotification(model.Subject, model.StartDate, model.EndDate, model.Message); } else { notification = nManager.GetNotificationById(model.Id); notification.Subject = model.Subject; notification.StartDate = model.StartDate; notification.EndDate = model.EndDate; notification.Message = model.Message; nManager.UpdateNotification(notification); } //save or update dependencies if (model.Id == 0) { foreach (KeyValuePair <long, List <string> > kp in dictionary) { foreach (string value in kp.Value) { nManager.CreateNotificationDependency(notification, Convert.ToInt64(kp.Key), value); } } } else { //remove dep. for (int i = 0; i < notification.NotificationDependency.Count(); i++) //foreach (NotificationDependency d in notification.NotificationDependency) { if (dictionary.ContainsKey(notification.NotificationDependency.ToList()[i].AttributeId)) { if (dictionary[notification.NotificationDependency.ToList()[i].AttributeId].Contains(notification.NotificationDependency.ToList()[i].DomainItem)) { //:) } else { notification.NotificationDependency.Remove(notification.NotificationDependency.ToList()[i]); nManager.UpdateNotification(notification); //nManager.DeleteNotificationDependency(d); } } else { //delete all dependencies notification.NotificationDependency.Clear(); nManager.UpdateNotification(notification); } } //add dep foreach (KeyValuePair <long, List <string> > kvp in dictionary) { long id = kvp.Key; foreach (string value in kvp.Value) { if (!notification.NotificationDependency.Any(a => a.AttributeId == id && a.DomainItem == value)) { nManager.CreateNotificationDependency(notification, Convert.ToInt64(id), value); } } } } if (notification.Id != 0 && model.Id == 0) { //Start -> add security ---------------------------------------- //31 is the sum from all rights: Read = 1, Write = 4, Delete = 8, Grant = 16 int fullRights = (int)RightType.Read + (int)RightType.Write + (int)RightType.Delete + (int)RightType.Grant; Entity entityType = entityTypeManager.FindByName("Notification"); //get admin groups: format= "groupname:resource structure attribute value" // give rights to group if fetch to notification string[] eventAdminGroups = Helper.Settings.get("EventAdminGroups").ToString().Split(','); Dictionary <string, string> adminGroupsDictionary = new Dictionary <string, string>(); if (eventAdminGroups != null && eventAdminGroups.Length > 0) { foreach (string group in eventAdminGroups) { string[] groupPair = group.Split(':'); adminGroupsDictionary.Add(groupPair[0], groupPair[1]); } } //get resource structrue attribute values to compare with admin group settings if (adminGroupsDictionary.Count > 0) { //get admin groups for notification var values = dictionary.SelectMany(pair => pair.Value).ToList(); var adminGroups = adminGroupsDictionary .Where(pair => values.Contains(pair.Value)) .Select(pair => pair.Key) .ToList(); using (var groupManager = new GroupManager()) { foreach (var g in adminGroups) { var group = groupManager.FindByNameAsync(g).Result; if (group != null) { if (pManager.GetRights(group.Id, entityType.Id, notification.Id) == 0) { pManager.Create(group.Id, entityType.Id, notification.Id, fullRights); } } } } } //rights to bexcis admin group using (var groupManager = new GroupManager()) { var adminGroup = groupManager.FindByNameAsync("administrator").Result; if (pManager.GetRights(adminGroup.Id, entityType.Id, notification.Id) == 0) { pManager.Create(adminGroup.Id, entityType.Id, notification.Id, fullRights); } } //rights to user that has create the notification var userTask = userManager.FindByNameAsync(HttpContext.User.Identity.Name); userTask.Wait(); var user = userTask.Result; pManager.Create(user, entityType, notification.Id, fullRights); //End -> add security ------------------------------------------ } //ToDo: Send email with notification to all in a affected schedule involved people if (affectedSchedules.Count > 0) { SendNotification(notification, affectedSchedules); } //return View("NotificationManager"); return(Json(new { success = true })); } else { List <AttributeDomainItemsModel> attributeDomainItems = (List <AttributeDomainItemsModel>)Session["FilterOptions"]; //Dictionary<long, List<string>> dictionary = (Dictionary<long, List<string>>)Session["ResourceFilter"]; if (dictionary != null) { foreach (AttributeDomainItemsModel m in attributeDomainItems) { for (int i = 0; i < m.DomainItems.Count(); i++) { var list = dictionary.SelectMany(x => x.Value); if (list.Contains(m.DomainItems[i].Key)) { m.DomainItems[i].Selected = true; } } } } model.AttributeDomainItems = attributeDomainItems; return(PartialView("_editNotification", model)); } } }
public ActionResult Save(EditNotificationModel model) { using (NotificationManager nManager = new NotificationManager()) using (EntityPermissionManager pManager = new EntityPermissionManager()) using (EntityManager entityTypeManager = new EntityManager()) using (UserManager userManager = new UserManager()) { Dictionary <long, List <string> > dictionary = (Dictionary <long, List <string> >)Session["ResourceFilter"]; if (ModelState.IsValid && dictionary != null) { //if edit need comarison between stored dependensies and set in session //get resource filter from the notification //Dictionary<long, List<string>> dictionary = (Dictionary<long, List<string>>)Session["ResourceFilter"]; Session["ResourceFilter"] = null; List <Schedule> affectedSchedules = GetAffectedSchedules(dictionary, model.StartDate, model.EndDate); Notification notification = new Rbm.Entities.Booking.Notification(); if (model.Id == 0) { notification = nManager.CreateNotification(model.Subject, model.StartDate, model.EndDate, model.Message); } else { notification = nManager.GetNotificationById(model.Id); notification.Subject = model.Subject; notification.StartDate = model.StartDate; notification.EndDate = model.EndDate; notification.Message = model.Message; nManager.UpdateNotification(notification); } //save or update dependencies if (model.Id == 0) { foreach (KeyValuePair <long, List <string> > kp in dictionary) { foreach (string value in kp.Value) { nManager.CreateNotificationDependency(notification, Convert.ToInt64(kp.Key), value); } } } else { //remove dep. for (int i = 0; i < notification.NotificationDependency.Count(); i++) //foreach (NotificationDependency d in notification.NotificationDependency) { if (dictionary.ContainsKey(notification.NotificationDependency.ToList()[i].AttributeId)) { if (dictionary[notification.NotificationDependency.ToList()[i].AttributeId].Contains(notification.NotificationDependency.ToList()[i].DomainItem)) { //:) } else { notification.NotificationDependency.Remove(notification.NotificationDependency.ToList()[i]); nManager.UpdateNotification(notification); //nManager.DeleteNotificationDependency(d); } } else { //delete all dependencies notification.NotificationDependency.Clear(); nManager.UpdateNotification(notification); } } //add dep foreach (KeyValuePair <long, List <string> > kvp in dictionary) { long id = kvp.Key; foreach (string value in kvp.Value) { if (!notification.NotificationDependency.Any(a => a.AttributeId == id && a.DomainItem == value)) { nManager.CreateNotificationDependency(notification, Convert.ToInt64(id), value); } } } } if (notification.Id != 0 && model.Id == 0) { //Start -> add security ---------------------------------------- { var userTask = userManager.FindByNameAsync(HttpContext.User.Identity.Name); userTask.Wait(); var user = userTask.Result; Entity entityType = entityTypeManager.FindByName("Notification"); //31 is the sum from all rights: Read = 1, Write = 4, Delete = 8, Grant = 16 int rights = (int)RightType.Read + (int)RightType.Write + (int)RightType.Delete + (int)RightType.Grant; pManager.Create(user, entityType, notification.Id, rights); } //End -> add security ------------------------------------------ } //ToDo: Send email with notification to all in a affected schedule involved people if (affectedSchedules.Count > 0) { SendNotification(notification, affectedSchedules); } //return View("NotificationManager"); return(Json(new { success = true })); } else { List <AttributeDomainItemsModel> attributeDomainItems = (List <AttributeDomainItemsModel>)Session["FilterOptions"]; //Dictionary<long, List<string>> dictionary = (Dictionary<long, List<string>>)Session["ResourceFilter"]; if (dictionary != null) { foreach (AttributeDomainItemsModel m in attributeDomainItems) { for (int i = 0; i < m.DomainItems.Count(); i++) { var list = dictionary.SelectMany(x => x.Value); if (list.Contains(m.DomainItems[i].Key)) { m.DomainItems[i].Selected = true; } } } } model.AttributeDomainItems = attributeDomainItems; return(PartialView("_editNotification", model)); } } }