コード例 #1
0
        public ActionResult Edit(UserAccountViewModel user)
        {
            UserAccount tempUser = db.UserAccounts.FirstOrDefault(x => x.UserAccountID == user.vm_UserAccount.UserAccountID);

            foreach (CurrentUserPermissionListItem cpli in user.vm_rList)
            {
                var oldRoleAssignment = tempUser.UserProjectRoles.FirstOrDefault(m => m.RoleId == cpli.RoleID);
                if (oldRoleAssignment != null && cpli.IsAssigned == false)   //user is currently assigned and the box was unchecked
                {
                    db.UserProjectRoles.Remove(oldRoleAssignment);
                }
                else if (oldRoleAssignment == null && cpli.IsAssigned == true) //user was not assigned and the box for this role was checked
                {
                    UserProjectRole newRole = new UserProjectRole();
                    newRole.UserAccountID = user.vm_UserAccount.UserAccountID;
                    newRole.RoleId        = cpli.RoleID;
                    db.UserProjectRoles.Add(newRole);
                }
            }

            tempUser.UserName      = user.vm_UserAccount.UserName;
            tempUser.FirstName     = user.vm_UserAccount.FirstName;
            tempUser.LastName      = user.vm_UserAccount.LastName;
            tempUser.Email         = user.vm_UserAccount.Email;
            tempUser.Locked        = user.vm_UserAccount.Locked;
            tempUser.UserAccountID = user.vm_UserAccount.UserAccountID;

            db.Entry(tempUser).State = EntityState.Modified;
            db.SaveChanges();
            TempData["Message"] = "User successfully updated";
            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public ActionResult Create(UserAccountViewModel user)
        {
            UserAccount tempUser = new UserAccount();

            foreach (CurrentUserPermissionListItem cpli in user.vm_rList)
            {
                if (cpli.IsAssigned == true) //user was not assigned and the box for this role was checked
                {
                    UserProjectRole newRole = new UserProjectRole();
                    newRole.UserAccountID = tempUser.UserAccountID;
                    newRole.RoleId        = cpli.RoleID;
                    db.UserProjectRoles.Add(newRole);
                }
            }

            tempUser.UserName      = user.vm_UserAccount.UserName;
            tempUser.FirstName     = user.vm_UserAccount.FirstName;
            tempUser.LastName      = user.vm_UserAccount.LastName;
            tempUser.Email         = user.vm_UserAccount.Email;
            tempUser.Locked        = user.vm_UserAccount.Locked == null ? 0 : user.vm_UserAccount.Locked;
            tempUser.UserAccountID = user.vm_UserAccount.UserAccountID;

            db.UserAccounts.Add(tempUser);
            db.SaveChanges();
            TempData["Message"] = "User successfully created";
            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public async Task <UserProjectRole> SaveUserProjectRole(UserProjectRole userProjectRole)
        {
            await db.AddAsync(userProjectRole);

            await db.SaveChangesAsync();

            return(userProjectRole);
        }
コード例 #4
0
        public async Task <UserProjectRole> DeleteUserProjectRole(UserProjectRole userProjectRole)
        {
            var userProjectRoleToUpdate = await GetUserProjectRoleById(userProjectRole.Id);

            userProjectRoleToUpdate.IsDeleted = true;
            await db.SaveChangesAsync();

            return(userProjectRoleToUpdate);
        }
コード例 #5
0
        public async Task <IActionResult> DeleteConfirmed(int id, UserProjectRole userProjectRole)
        {
            if (id != userProjectRole.Id)
            {
                return(NotFound());
            }
            await _generalServices.DeleteUserProjectRole(userProjectRole);

            return(RedirectToAction(nameof(Index)));
        }
コード例 #6
0
        public async Task <IActionResult> Create(UserProjectRole userProjectRole)
        {
            if (ModelState.IsValid)
            {
                await _generalServices.SaveUserProjectRole(userProjectRole);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(userProjectRole));
        }
コード例 #7
0
        public ProjectAuthorizeFilterTests()
        {
            _dbContext = new ApplicationContext(new DbContextOptions<ApplicationContext>());

            var user1 = new User { UserName = "******" };
            var user2 = new User { UserName = "******" };

            var project1 = new Project("project1","","",user1);
            var project2 = new Project ("project2", "","", user1);
            var project3 = new Project ("project3","","",user2);

            var userRole1 = new UserProjectRole
            {
                User = user1,
                Project = project1,
                Role = new Role { Name = Role.Value.Creator.ToString()}
            };

            var userRole2 = new UserProjectRole
            {
                User = user1,
                Project = project2,
                Role = new Role { Name = Role.Value.Creator.ToString() }
            };

            var userRole3 = new UserProjectRole
            {
                User = user2,
                Project = project3,
                Role = new Role { Name = Role.Value.Creator.ToString() }
            };

            var userRole4 = new UserProjectRole
            {
                User = user2,
                Project = project2,
                Role = new Role { Name = Role.Value.Author.ToString() }
            };

            _dbContext.Projects = GetQueryableMockDbSet(new Project[] 
                { 
                    project1,project2,project3
                });

            _dbContext.UserRoles = GetQueryableMockDbSet(new UserProjectRole[] 
            {
                userRole1,userRole2,userRole3,userRole4
            });
        }
コード例 #8
0
        public ActionResult Update(string accountUsername, int id, EditProjectViewModel model)
        {
            if (ModelState.IsValid)
            {
                List <AspNetUser> users           = db.AspNetUsers.ToList();
                List <AspNetRole> roles           = db.AspNetRoles.ToList();
                AspNetUser        user            = users.FirstOrDefault(u => u.UserName == accountUsername);
                UserProjectRole   userProjectRole = db.UserProjectRoles.FirstOrDefault(p => p.ProjectID == model.ID);
                Project           project         = db.Projects.FirstOrDefault(p => p.ID == userProjectRole.ProjectID);

                // Update Project Manager, if needed.
                if (project.Manager != model.Manager)
                {
                    project.Name    = model.Name;
                    project.Manager = model.Manager;
                    var projectManagerRoleID = roles.FirstOrDefault(r => r.Name == "Project Manager").Id;
                    var projectManager       = users.FirstOrDefault(u => u.Id == project.Manager);

                    var projectManagerRoleToRemove = db.UserProjectRoles.Where(x => x.ProjectID == project.ID && x.RoleID == projectManagerRoleID);
                    db.UserProjectRoles.RemoveRange(projectManagerRoleToRemove);
                    db.UserProjectRoles.Add(new UserProjectRole
                    {
                        UserID    = project.Manager,
                        ProjectID = project.ID,
                        RoleID    = projectManagerRoleID
                    });
                }

                // Update Developers
                var projectDeveloperRoleID        = roles.FirstOrDefault(r => r.Name == "Developer").Id;
                var projectDeveloperRolesToRemove = db.UserProjectRoles.Where(x => x.ProjectID == project.ID && x.RoleID == projectDeveloperRoleID);
                db.UserProjectRoles.RemoveRange(projectDeveloperRolesToRemove);
                foreach (var item in model.SelectedUsers)
                {
                    db.UserProjectRoles.Add(new UserProjectRole
                    {
                        UserID    = item,
                        ProjectID = project.ID,
                        RoleID    = projectDeveloperRoleID
                    });
                }

                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Show", new { id = id }));
            }
            return(View(model));
        }
コード例 #9
0
        /// <summary>
        /// Assign author to project but if author is not registered
        /// add his email to database to assign him later.
        /// </summary>
        /// <param name="author">Author email and project.</param>
        /// <returns></returns>
        public async Task Assign(UnauthorizedAuthor author)
        {
            bool isAlreadyInProject = Context.Users
                                      .Where(user => user.ProjectRoles
                                             .Any(projectRole => projectRole.Project.Id == author.Project.Id))
                                      .Any(user => user.Email == author.Email);

            if (isAlreadyInProject)
            {
                return;
            }

            User user = await Context.Users
                        .Include(user => user.ProjectRoles)
                        .FirstOrDefaultAsync(user => user.Email == author.Email);

            if (user != null)
            {
                Role authorRole = await Context.Roles.FirstAsync(role => role.Name == Role.Value.Author.ToString());

                var projectRole = new UserProjectRole
                {
                    Project = author.Project,
                    Role    = authorRole
                };

                user.ProjectRoles = user.ProjectRoles.ToList();
                ((List <UserProjectRole>)user.ProjectRoles).Add(projectRole);


                Context.Users.Update(user);
                await Context.SaveChangesAsync();
            }
            else
            {
                bool isAssigned = Context.UnauthorizedAuthors
                                  .Any(unathorized => unathorized.Email == author.Email &&
                                       unathorized.ProjectId == author.Project.Id);

                if (isAssigned == false)
                {
                    Context.UnauthorizedAuthors.Add(author);
                    await Context.SaveChangesAsync();
                }
            }
        }
コード例 #10
0
        public HttpResponseMessage Add(HttpRequestMessage request, ProjectViewModel project)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;

                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest,
                                                      ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                                                      .Select(m => m.ErrorMessage).ToArray());
                }
                else
                {
                    //if (_projectsRepository.UserExists(project.Email, project.IdentityCard))
                    //{
                    //    ModelState.AddModelError("Invalid user", "Email or Identity Card number already exists");
                    //    response = request.CreateResponse(HttpStatusCode.BadRequest,
                    //    ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                    //          .Select(m => m.ErrorMessage).ToArray());
                    //}
                    //else
                    //{
                    //}

                    Project newProject = new Project();
                    newProject.UpdateProject(project);
                    _projectsRepository.Add(newProject);
                    _unitOfWork.Commit();

                    UserProjectRole role = new UserProjectRole();
                    role.ProjectId = newProject.ID;
                    role.UserId = RequestContext.Principal.Identity.Name;
                    _userProjectRolesRepository.Add(role);
                    _unitOfWork.Commit();

                    // Update view model
                    project = Mapper.Map <Project, ProjectViewModel>(newProject);
                    response = request.CreateResponse <ProjectViewModel>(HttpStatusCode.Created, project);
                }

                return response;
            }));
        }
コード例 #11
0
        /// <summary>
        /// Delete author from project.
        /// </summary>
        /// <param name="request">Author name and project name.</param>
        /// <returns> Nothing.</returns>
        public async Task <Unit> Handle(UserHandlerData <AuthorDeletingFormData, Unit> request, CancellationToken cancellationToken)
        {
            User user = await Context.Users
                        .Include(user => user.ProjectRoles)
                        .ThenInclude(projectRole => projectRole.Project)
                        .FirstAsync(user => user.UserName == request.Data.UserName);

            UserProjectRole projectRole = user.ProjectRoles
                                          .First(projectRole => projectRole.Project.Name == request.Data.ProjectName &&
                                                 projectRole.Project.Creator.UserName == request.User.UserName);

            /// Deleting user role author for current project.
            user.ProjectRoles = user.ProjectRoles.ToList();
            ((List <UserProjectRole>)user.ProjectRoles).Remove(projectRole);

            Context.Users.Update(user);
            await Context.SaveChangesAsync();

            return(Unit.Value);
        }
コード例 #12
0
        public async Task <IActionResult> Edit(int id, UserProjectRole userProjectRole)
        {
            if (id != userProjectRole.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await _generalServices.UpdateUserProjectRole(userProjectRole);
                }
                catch (Exception ex)
                {
                    throw;
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(userProjectRole));
        }
コード例 #13
0
        private void addUserToProjectRole(User user, int projectId, int projectRoleId)
        {
            var project = _projectRepository.GetSingle(projectId);
            var role    = _projectRoleRepository.GetSingle(projectRoleId);

            if (project == null)
            {
                throw new ApplicationException("Project doesn't exist.");
            }

            if (role == null)
            {
                throw new ApplicationException("Role doesn't exist.");
            }

            var userProjectRole = new UserProjectRole()
            {
                UserId        = user.ID,
                ProjectId     = project.ID,
                ProjectRoleId = role.ID
            };

            _userProjectRoleRepository.Add(userProjectRole);
        }