/// <summary> /// removes user from issue /// </summary> /// <param name="accessRight">access right to be removed</param> /// <param name="userId">user who is delteing access right</param> /// <returns>bool if successful</returns> public static bool RemoveAccessRight(AccessRight accessRight, int userId) { ApplicationDBEntities ctx = new ApplicationDBEntities(); try { using (var dbContextTransaction = ctx.Database.BeginTransaction()) { ctx.Database.ExecuteSqlCommand("delete from [appSchema].[HAccessRight] WHERE UserId = {0} AND IssueId ={1}", accessRight.UserId, accessRight.IssueId); ctx.Database.ExecuteSqlCommand("delete from [appSchema].[AccessRight] WHERE UserId = {0} AND IssueId ={1}", accessRight.UserId, accessRight.IssueId); dbContextTransaction.Commit(); HAccessRight har = new HAccessRight(); har.ChangeDate = DateTime.Now; har.IssueId = accessRight.IssueId; har.UserId = userId; User u = ctx.User.Find(accessRight.UserId); u = ctx.User.Find(accessRight.UserId); har.Action = u.FirstName + " " + u.LastName + " removed"; har.SelfAssesmentDescr = ""; har.SelfAssessmentValue = 0; ctx.HAccessRight.Add(har); ctx.Entry(har).State = EntityState.Added; ctx.SaveChanges(); } ctx.Dispose(); return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
/// <summary> /// updates slefassesment of an User /// </summary> /// <param name="value">self assessment value</param> /// <param name="description">self assessment description</param> public static void UpdateSelfAssesment(double value, string description, int issueId, int userId) { ApplicationDBEntities ctx = new ApplicationDBEntities(); AccessRight right = ctx.AccessRight.AsNoTracking().Where(x => x.IssueId == issueId && x.UserId == userId).FirstOrDefault(); bool update = false; if (right.SelfAssessmentValue != value) { right.SelfAssessmentValue = value; update = true; } if (right.SelfAssesmentDescr != description) { right.SelfAssesmentDescr = description; update = true; } if (update) { HAccessRight har = new HAccessRight(); har.SelfAssesmentDescr = right.SelfAssesmentDescr; har.SelfAssessmentValue = right.SelfAssessmentValue; har.ChangeDate = System.DateTime.Now; har.IssueId = right.IssueId; har.UserId = right.UserId; har.Action = "Selfassessment updated"; ctx.HAccessRight.Add(har); ctx.Entry(har).State = EntityState.Added; ctx.Entry(right).State = EntityState.Modified; ctx.SaveChanges(); } ctx.Dispose(); }
/// <summary> /// adds user to issue /// </summary> /// <param name="accessRight">access right</param> /// <param name="userId">user who is adding oder user</param> /// <returns>true if successful</returns> public static bool AddAccessRight(AccessRight accessRight, int userId) { ApplicationDBEntities ctx = new ApplicationDBEntities(); accessRight.MailNotification = false; accessRight.NotificationLevel = ""; accessRight.SelfAssessmentValue = 0; ctx.AccessRight.Add(accessRight); ctx.Entry(accessRight).State = EntityState.Added; User u = ctx.User.Find(accessRight.UserId); HAccessRight har = new HAccessRight(); har.ChangeDate = DateTime.Now; har.IssueId = accessRight.IssueId; har.UserId = userId; har.Action = u.FirstName + " " + u.LastName + " added"; har.SelfAssesmentDescr = ""; har.SelfAssessmentValue = 0; ctx.HAccessRight.Add(har); ctx.Entry(har).State = EntityState.Added; try { ctx.SaveChanges(); } catch (DbEntityValidationException ex) { Console.WriteLine(ex.Message); ctx.AccessRight.Remove(accessRight); return(false); } catch (Exception ex) { Console.WriteLine(ex.Message); ctx.AccessRight.Remove(accessRight); return(false); } GrantAccess(accessRight.UserId, accessRight.IssueId, ctx); ctx.Dispose(); return(true); }
/// <summary> /// updates the accessrights for an issue /// </summary> /// <param name="addedList">new access rights added to issue</param> /// <param name="deletedList">accessrights which should be deleted</param> /// <param name="editedList">list of edited access rights</param> /// <param name="issueId"></param> /// <param name="userId"></param> public static void UpdateRights(List <AccessRight> addedList, List <AccessRight> deletedList, List <AccessRight> editedList, int issueId, int userId) { ApplicationDBEntities ctx = new ApplicationDBEntities(); User u; List <AccessRight> intAddList = addedList.Intersect(ctx.AccessRight).ToList(); deletedList = deletedList.Distinct().ToList(); addedList = addedList.Except(intAddList).ToList(); foreach (AccessRight ar in addedList) { ar.IssueId = issueId; ar.MailNotification = false; ar.NotificationLevel = ""; ar.SelfAssesmentDescr = ""; ar.SelfAssessmentValue = 0; ctx.AccessRight.Add(ar); ctx.Entry(ar).State = EntityState.Added; HAccessRight har = new HAccessRight(); har.ChangeDate = DateTime.Now; har.IssueId = ar.IssueId; har.UserId = userId; u = ctx.User.Find(ar.UserId); har.Action = u.FirstName + " " + u.LastName + " added"; har.SelfAssesmentDescr = ""; har.SelfAssessmentValue = 0; ctx.HAccessRight.Add(har); ctx.Entry(har).State = EntityState.Added; try { ctx.SaveChanges(); } catch (DbEntityValidationException ex) { Console.WriteLine(ex.Message); ctx.AccessRight.Remove(ar); } catch (Exception ex) { Console.WriteLine(ex.Message); ctx.AccessRight.Remove(ar); } GrantAccess(ar.UserId, issueId, ctx); } foreach (AccessRight ar in deletedList) { if (ctx.AccessRight.Where(x => x.UserId == ar.UserId && x.IssueId == issueId).Count() > 0) { using (var dbContextTransaction = ctx.Database.BeginTransaction()) { ctx.Database.ExecuteSqlCommand("delete from [appSchema].[AccessRight] WHERE UserId = {0} AND IssueId ={1}", ar.UserId, issueId); dbContextTransaction.Commit(); } } } if (editedList == null) { return; } AccessRight tmp; foreach (AccessRight ar in editedList) { if (ar.IssueId != 0) { tmp = ctx.AccessRight.AsNoTracking().Where(x => x.UserId == ar.UserId && x.IssueId == issueId).FirstOrDefault(); if (tmp != null && tmp.Right != ar.Right) { tmp.Right = ar.Right; ctx.Entry(tmp).State = EntityState.Modified; ctx.SaveChanges(); } } } ctx.Dispose(); }
/// <summary> /// inserts new issue /// </summary> /// <param name="issue"></param> /// <param name="userId">user who is performing operation</param> /// <returns>id of inserted issue</returns> public static int InsertIssue(Issue issue, int userId, double selfAssessmentValue, string selfAssessmentDescription) { int issueId = -1; ApplicationDBEntities ctx = new ApplicationDBEntities(); try { Issue updateIssue = ctx.Issue.Create(); updateIssue.Title = issue.Title; updateIssue.Description = issue.Description; updateIssue.AnonymousPosting = issue.AnonymousPosting; updateIssue.Status = issue.Status; updateIssue.Parent = issue.Parent; updateIssue.DependsOn = issue.DependsOn; updateIssue.Setting = issue.Setting; //updateIssue.TagIssue = null; ctx.Issue.Add(updateIssue); ctx.Entry(updateIssue).State = EntityState.Added; ctx.SaveChanges(); issueId = updateIssue.Id; AccessRight ar = new AccessRight(); ar.IssueId = issueId; ar.UserId = userId; ar.Right = "O"; ar.SelfAssessmentValue = selfAssessmentValue; ar.SelfAssesmentDescr = selfAssessmentDescription; ar.MailNotification = false; ctx.AccessRight.Add(ar); ctx.Entry(ar).State = EntityState.Added; HAccessRight har = new HAccessRight(); har.ChangeDate = DateTime.Now; har.IssueId = ar.IssueId; har.UserId = userId; har.Action = "selfassessment added"; har.SelfAssesmentDescr = selfAssessmentDescription; har.SelfAssessmentValue = selfAssessmentValue; ctx.HAccessRight.Add(har); ctx.Entry(har).State = EntityState.Added; HIssue hissue = new HIssue(); hissue.ChangeDate = DateTime.Now; hissue.IssueId = issueId; hissue.UserId = userId; hissue.Action = "issue created"; hissue.Status = issue.Status; hissue.Title = issue.Title; hissue.Description = issue.Description; hissue.Setting = issue.Setting; hissue.Status = issue.Status; hissue.AnonymousPosting = issue.AnonymousPosting; hissue.Parent = issue.Parent; hissue.DependsOn = issue.DependsOn; hissue.GroupThink = issue.GroupThink; hissue.ReviewRating = issue.ReviewRating; ctx.HIssue.Add(hissue); ctx.Entry(hissue).State = EntityState.Added; ctx.SaveChanges(); //mark issue as read ApplicationDBEntities ctx2 = new ApplicationDBEntities(); DbCommand cmd = ctx2.Database.Connection.CreateCommand(); ctx2.Database.Connection.Open(); cmd.CommandText = "UPDATE appSchema.InformationRead SET [Read] = 1 WHERE UserId = " + userId + " AND TName LIKE 'Alternative' AND FK LIKE '" + issueId + "'"; cmd.CommandType = System.Data.CommandType.Text; cmd.ExecuteNonQuery(); ctx2.Database.Connection.Close(); ctx2.Dispose(); } catch (DbEntityValidationException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } ctx.Dispose(); return(issueId); }