예제 #1
0
        public async Task <IActionResult> Delete([FromRoute] int departmentId, [FromBody] string userId)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByIdAsync(userId);

            var department = _departmentRepository.Get(departmentId);

            if (user == null)
            {
                return(NotFound("User is not found"));
            }

            if (department == null)
            {
                return(NotFound("Department is not found"));
            }

            var depUser = new DepartmentApplicationUser
            {
                ApplicationUserId = user.Id,
                DepartmentId      = department.DepartmentId,
                IsMaintainer      = false
            };

            await _departmentApplicationUserRepository.Remove(userId, department.DepartmentId);

            return(CreatedAtAction("Get", new { id = department.DepartmentId }, department));
        }
        /// <summary>
        /// When user is granted to be a maintainer to current Department
        /// Update isMaintainer in DepartmentApplicationUsers to current Department
        /// Add DepartmentRoles to the Current user.
        ///
        /// When remove Department Maintainer roles, will remove isMaintainer and Role.
        /// </summary>
        /// <param name="departmentApplicationUserView">Vale with isMaintainer, Department and username</param>
        public async Task UpdateMaintainDepartment(DepartmentApplicationUserViewModel departmentApplicationUserView)
        {
            //Update the DepartmentApplicationUser to isMaintainer to current user
            DepartmentApplicationUser departmentApplicationUser = new DepartmentApplicationUser
            {
                ApplicationUserId = departmentApplicationUserView.ApplicationUserId,
                DepartmentId      = departmentApplicationUserView.DepartmentId,
                IsMaintainer      = departmentApplicationUserView.IsMaintainer
            };

            _db.DepartmentApplicationUsers.Update(departmentApplicationUser);
            await _db.SaveChangesAsync();

            var currentUser = _userManager.FindByIdAsync(departmentApplicationUser.ApplicationUserId).Result;
            var roleName    = Roles.DepartmentMaintainer;

            //If user is not in DepartmentRoles so add to role or removce.
            if (departmentApplicationUser.IsMaintainer)
            {
                var isInRole = await _userManager.IsInRoleAsync(currentUser, "Department Maintainer");

                if (!isInRole)
                {
                    await _userManager.AddToRoleAsync(currentUser, roleName);
                }
            }
            else
            {
                var isUserMaintainer = _db.DepartmentApplicationUsers.Any(x =>
                                                                          x.IsMaintainer && x.ApplicationUserId == currentUser.Id);
                if (!isUserMaintainer)
                {
                    await _userManager.RemoveFromRoleAsync(currentUser, roleName);
                }
            }
        }
예제 #3
0
        public async Task Add(DepartmentApplicationUser departmentApplicationUser)
        {
            await _db.DepartmentApplicationUsers.AddAsync(departmentApplicationUser);

            await _db.SaveChangesAsync();
        }
예제 #4
0
 public Task Save(DepartmentApplicationUser departmentApplicationUser)
 {
     throw new System.NotImplementedException();
 }