void EditDetails(int?id) { // нельзя редактировать организацию, если ты не админ bool editOrganization = true; if (id.HasValue) { IBoard board = Utility.Boards.Get(id.Value); ViewData.Model = board; if (board.OrganizationId.HasValue) { ViewData.Add("Organization", Utility.Organizations.Get(board.OrganizationId.Value)); EmployeeSettings userSettings = Utility.Organizations.GetUserSettings(board.OrganizationId.Value, Utility.Users.CurrentUser.Id); editOrganization = userSettings.Settings.IsAdmin; } } ViewData.Add("EditOrganization", editOrganization); if (editOrganization) { // только там, где пользователь админ, он может привязать доску IEnumerable <IOrganization> organizations = Utility.Organizations .GetByUser(Utility.Authentication.UserId) .Where(x => x.Settings.GetUserRole().HasTheFlag(EmployeeRole.Administrator)) .Select(x => x.Organization) .ToList(); ViewData.Add("organizations", organizations); } }
public PartialViewResult EmployeeEdit(int organizationId, int id) { EmployeeSettings settings = Utility.Organizations.GetUserSettings(organizationId, id); ViewData.Model = settings; return(PartialView()); }
public void Update(int boardId, string name, string description, int?refreshPeriod, IOrganization organization) { using (TransactionScope scope = new TransactionScope()) { IBoard board = Repository.Boards.Get(boardId); int? organizationId = organization != null ? organization.Id : (int?)null; if (board.OrganizationId != organizationId) { CheckLimits(organization); } board.Name = name; board.Description = description; board.RefreshPeriod = refreshPeriod; if (board.OrganizationId.HasValue && board.OrganizationId != organizationId) { EmployeeSettings userSettings = Utility.Organizations.GetUserSettings(board.OrganizationId.Value, Utility.Users.CurrentUser.Id); if (!userSettings.Settings.IsAdmin) { throw new InvalidOperationTimezException("Нет прав на изменение организации."); } } board.OrganizationId = organizationId; Repository.SubmitChanges(); OnUpdate.Invoke(new EventArgs <IBoard>(board)); scope.Complete(); } }
public void Edit(EmployeeSettings employeeSettings) { using (var context = new ElysiumContext()) { var dbEmployeeSettings = context.EmployeeSettings.Find(employeeSettings.Id); context.Entry(dbEmployeeSettings).CurrentValues.SetValues(employeeSettings); context.SaveChanges(); } }
public void Add(EmployeeSettings employeeSettings) { using (var context = new ElysiumContext()) { context.EmployeeSettings.Add(employeeSettings); context.SaveChanges(); } }
private static void CheckOneFree(IEnumerable <EmployeeSettings> organizations) { EmployeeSettings settings = organizations.FirstOrDefault(x => x.Organization.IsFree); if (settings != null) { IUser user = settings.User; throw new CanBeOnlyOneFreeException(user); } }
public void Delete(Guid Id) { using (var context = new ElysiumContext()) { var employeeSettings = new EmployeeSettings() { Id = Id }; context.EmployeeSettings.Remove(employeeSettings); context.SaveChanges(); } }
public void Delete(Guid id) { using (var context = new ElysiumContext()) { var employee = new Employee() { Id = id }; var settings = new EmployeeSettings { Id = id }; context.Entry(settings).State = EntityState.Deleted; context.SaveChanges(); context.Employee.Attach(employee); context.Entry(employee).State = EntityState.Deleted; context.SaveChanges(); } }
/// <summary> /// Проверка прав в организации /// </summary> private ResultType?НasAccessToOrganization(ActionExecutingContext filterContext) { OrganizationPermissionAttribute attribute = (OrganizationPermissionAttribute)filterContext .ActionDescriptor .GetCustomAttributes(typeof(OrganizationPermissionAttribute), false) .FirstOrDefault(); if (attribute != null) { int?organizationId = (int?)filterContext.ActionParameters[attribute.IdParamName]; if (organizationId.HasValue) { ViewData.Add("CurrentOrganizationId", organizationId.Value); EmployeeSettings employeeSettings = Utility.Organizations .GetUserSettings(organizationId.Value, Utility.Authentication.UserId); if (employeeSettings == null) { return(attribute.ResultType); } EmployeeRole userRole = employeeSettings.Settings.GetUserRole(); ViewData.Add("RoleInOrganization", userRole); bool hasAccess = false; foreach (EmployeeRole roles in attribute.Roles) { hasAccess |= userRole.HasTheFlag(roles); if (hasAccess) { break; } } return(hasAccess ? (ResultType?)null : attribute.ResultType); } } return(null); }
public void OrganizationTest() { FormCollection collection = new FormCollection(); ViewResult result; RedirectToRouteResult redirectToRouteResult; OrganizationController organizationController = Base.GetController <OrganizationController>(); PartialViewResult partialViewResult = organizationController.Edit(null); List <ITariff> tariffs = partialViewResult.ViewData.Get <List <ITariff> >("tariffs"); Assert.IsNotNull(tariffs); ITariff freeTariff = tariffs.Single(x => x.IsFree()); Assert.IsNotNull(freeTariff); Main.Registation(Email0); // создали тестовую организацию collection.Clear(); organizationController = Base.GetController <OrganizationController>(); collection.Add("Name", "test"); collection.Add("TariffId", freeTariff.Id.ToString()); partialViewResult = organizationController.Edit(null, collection); IOrganization organization = ((List <EmployeeSettings>)partialViewResult.Model).Single().Organization; Assert.IsNotNull(organization); int organizationId = organization.Id; #region Приглашения InviteController inviteController = Base.GetController <InviteController>(); // через мыло незареганного collection.Clear(); collection.Add("Email", Email3); inviteController.NewInvite(organizationId, collection); List <IUsersInvite> invites = inviteController.Utility.Invites.GetInvites(organizationId); IUsersInvite invite = invites.FirstOrDefault(x => x.EMail.ToUpper() == Email3.ToUpper()); Assert.IsNotNull(invite); Main.Registation(Email3, out result, out redirectToRouteResult, invite.InviteCode); ViewResultBase resultBase = organizationController.EmployeeList(organizationId); EmployeeSettings emeil3User = (resultBase.Model as List <EmployeeSettings>).FirstOrDefault(x => x.User.EMail.ToUpper() == Email3.ToUpper()); Assert.IsNotNull(emeil3User); collection.Clear(); collection.Add("delete", "true"); organizationController.EmployeeEdit(organizationId, emeil3User.User.Id, collection); // через мыло зареганного inviteController.Dispose(); inviteController = Base.GetController <InviteController>(); collection.Clear(); collection.Add("Email", Email3); inviteController.NewInvite(organizationId, collection); invites = organizationController.Utility.Invites.GetInvites(organizationId); invite = invites.FirstOrDefault(x => x.EMail.ToUpper() == Email3.ToUpper()); Assert.IsNotNull(invite); Base.GetController <AdminController>().ClearCache(); resultBase = organizationController.EmployeeList(organizationId); emeil3User = (resultBase.Model as List <EmployeeSettings>).FirstOrDefault(x => x.User.EMail.ToUpper() == Email3.ToUpper()); Assert.IsNotNull(emeil3User); var userController = Base.GetController <UserController>(); userController.Login(null, Email3, Email3, true, null); inviteController.AcceptInvite(organizationId); emeil3User = (resultBase.Model as List <EmployeeSettings>).FirstOrDefault(x => x.User.EMail.ToUpper() == Email3.ToUpper()); Assert.IsTrue(emeil3User.Settings.UserRole == (int)EmployeeRole.Employee); // через ссылку незареганного пользователя userController.Dispose(); userController = Base.GetController <UserController>(); userController.SignOut(); redirectToRouteResult = (RedirectToRouteResult)userController.Invite(organization.InviteCode); Assert.IsTrue(redirectToRouteResult.RouteValues["action"].ToString() == "Register"); collection.Clear(); Main.Registation(Email4, out result, out redirectToRouteResult, organization.InviteCode); resultBase = inviteController.List(); var organizations = (IEnumerable <IOrganization>)resultBase.Model; IOrganization first = organizations.FirstOrDefault(); Assert.IsTrue(first != null && first.Id == organizationId); inviteController.AcceptInvite(first.Id); resultBase = organizationController.EmployeeList(organizationId); EmployeeSettings emeil4User = ((List <EmployeeSettings>)resultBase.Model).FirstOrDefault(x => x.User.EMail.ToUpper() == Email4.ToUpper()); Assert.IsNotNull(emeil4User); collection.Clear(); collection.Add("delete", "true"); organizationController.EmployeeEdit(organizationId, emeil4User.User.Id, collection); // через ссылку зареганного пользователя userController.Dispose(); userController = Base.GetController <UserController>(); redirectToRouteResult = (RedirectToRouteResult)userController.Invite(organization.InviteCode); Assert.IsTrue(redirectToRouteResult.RouteValues["action"].ToString() == "Index"); collection.Clear(); inviteController.Dispose(); inviteController = Base.GetController <InviteController>(); resultBase = inviteController.List(); organizations = (IEnumerable <IOrganization>)resultBase.Model; first = organizations.FirstOrDefault(); Assert.IsTrue(first != null && first.Id == organizationId); inviteController.AcceptInvite(first.Id); resultBase = organizationController.EmployeeList(organizationId); emeil4User = (resultBase.Model as List <EmployeeSettings>).FirstOrDefault(x => x.User.EMail.ToUpper() == Email4.ToUpper()); Assert.IsNotNull(emeil4User); #endregion #region Удаление userController.Dispose(); userController = Base.GetController <UserController>(); userController.Login(null, Email3, Email3, true, null); BoardsController boardsController = Base.GetController <BoardsController>(); collection.Clear(); collection.Add("name", "t1"); collection.Add("OrganizationId", organizationId.ToString()); boardsController.Create(collection); collection.Clear(); collection.Add("name", "t2"); collection.Add("OrganizationId", organizationId.ToString()); boardsController.Create(collection); collection.Clear(); organizationController = Base.GetController <OrganizationController>(); organizationController.Delete(organizationId); boardsController = Base.GetController <BoardsController>(); partialViewResult = boardsController.List(null); Assert.IsFalse((partialViewResult.Model as List <IBoard>).Single().OrganizationId.HasValue); // остается только личная доска #endregion }
private EmployeeSettingsDto MapToDto(EmployeeSettings employeeSettings) { return(ToDtoMapper.Map <EmployeeSettingsDto>(employeeSettings)); }