public async Task <RoleForListDto> DeleteRoleFromUser(int userId, int roleId)
        {
            var role = await roleDal.GetAsync(x => x.Id == roleId);

            if (role == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { RolesListNotFound = Messages.RoleListNotFound });
            }

            var user = await userDal.GetAsync(x => x.Id == userId);

            if (user == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { UserNotFound = Messages.UserNotFound });
            }

            var userRole = await userRoleDal.GetAsync(x => x.RoleId == roleId && x.UserId == userId);

            if (userRole == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { UserRoleNotFound = Messages.NotFound });
            }


            await userRoleDal.Delete(userRole);

            return(mapper.Map <Role, RoleForListDto>(role));
        }
Esempio n. 2
0
        public IResult AddWorker(Worker worker)
        {
            try
            {
                worker.HireDate = DateTime.Now;
                worker.QuitDate = new DateTime(1900, 01, 01, 0, 0, 0);
                _workerDal.Add(worker);

                var userRole = new UserRole
                {
                    RoleId = 2, //worker rolü
                    UserId = worker.WorkerId
                };
                var oldRole = _userRoleDal.Get(ur => ur.UserId == worker.WorkerId);
                _userRoleDal.Delete(oldRole);
                _userRoleDal.Add(userRole);
                return(new SuccessResult());
            }
            catch (Exception)
            {
                return(new ErrorResult(Messages.ErrorWhileAddingEntity));
            }
        }
Esempio n. 3
0
        public async Task <UserForListDto> Update(UserForRegisterDto userForRegisterDto)
        {
            var userFromRepo = await userDal.GetAsync(x => x.Email == userForRegisterDto.Email);

            if (userFromRepo == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { UserNotFound = Messages.UserNotFound });
            }

            var userForUpdate = mapper.Map(userForRegisterDto, userFromRepo);



            var userUpdate = await userDal.Update(userForUpdate);

            var spec = new UserWithCampusAndDepartmentAndDegreeSpecification(userUpdate.Email);
            var user = await userDal.GetEntityWithSpecAsync(spec);

            var chekcPublicRoles = await announceStatusCheck.CheckPublicRole(roleDal, roleCategoryDal);

            var userRolePublic = await userRoleDal.GetAsync(x => x.Role.Name.ToLower() == "public");

            if (user.IsActive)
            {
                if (userRolePublic == null)
                {
                    userRolePublic = new UserRole
                    {
                        UserId = user.Id,
                        RoleId = chekcPublicRoles.Id
                    };
                    await userRoleDal.Add(userRolePublic);
                }
            }
            else if (!user.IsActive)
            {
                if (userRolePublic != null)
                {
                    await userRoleDal.Delete(userRolePublic);
                }
            }
            return(mapper.Map <User, UserForListDto>(user));
        }
Esempio n. 4
0
        public async Task <UserWithRolesDto> EditUserRoles(UserEditRolesDto userEditRolesDto)
        {
            var userRolesFromRepo = await userDal.GetUserWithRoles(userEditRolesDto.UserId);

            var mappedUserRoles = mapper.Map <UserWithRolesDto>(userRolesFromRepo);
            var userFromRepo    = await userDal.Get(u => u.Id == userEditRolesDto.UserId);

            var selectedRoles = userEditRolesDto.RoleId;

            selectedRoles = selectedRoles ?? new int[] {};

            var addroles = new UserRole();

            foreach (var item in selectedRoles.Except(mappedUserRoles.UserRoles.Select(p => p.Id)))
            {
                addroles = await userRoleDal.Add(new UserRole { UserId = userFromRepo.Id, RoleId = item });
            }

            if (addroles == null)
            {
                throw new Exception("Can't added user roles.!!");
            }

            var  userroles   = mappedUserRoles.UserRoles.Select(p => p.Id);
            bool removeRoles = false;

            foreach (var item in userroles.Except(selectedRoles))
            {
                removeRoles = await userRoleDal.Delete(new UserRole { UserId = userFromRepo.Id, RoleId = item });
            }
            if (!removeRoles)
            {
                throw new Exception("Could not remove old user roles.!!");
            }

            return(mapper.Map <UserWithRolesDto>(await userDal.GetUserWithRoles(userFromRepo.Id)));
        }
Esempio n. 5
0
 public void Delete(UserRole userRole)
 {
     _userRoleDal.Delete(userRole);
 }
Esempio n. 6
0
 public void Delete(int userRoleId)
 {
     _userRoleDal.Delete(new UserRole {
         Id = userRoleId
     });
 }
Esempio n. 7
0
 public void Delete(UserRole entity)
 {
     _userRoleDal.Delete(entity);
 }