Ejemplo n.º 1
0
        public bool AssignRolesToUser(AddRoleToUserTO rolesToAssign)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var result = _authRepository.AssignRolesToUser(rolesToAssign.UserName, rolesToAssign.Roles);

                if (result)
                {
                    scope.Complete();
                    return(true);
                }

                return(false);
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> AssignRolesToUser(AddRoleToUserTO rolesToAssign)
        {
            foreach (var item in rolesToAssign.Roles)
            {
                bool roleExists = await _authService.CheckIfRoleExists(item);

                if (!roleExists)
                {
                    logger.Log(LogLevel.Info, "Role: " + item + " doesn't exist!\n");
                    return(BadRequest(UserENUM.ROLE_NOT_FOUND.ToString()));
                }
            }

            var userRoles = _authService.GetUserRoles(rolesToAssign.UserName);

            foreach (var role in userRoles)
            {
                if (rolesToAssign.Roles.Contains(role))
                {
                    return(BadRequest(UserENUM.ROLE_ALREADY_ADDED_FOR_USER.ToString()));
                }
            }

            bool result = _authService.AssignRolesToUser(rolesToAssign);

            if (result)
            {
                foreach (var item in rolesToAssign.Roles)
                {
                    logger.Log(LogLevel.Info, "Role: " + item + " was added for user: "******".\n");
                }

                return(Ok(result));
            }

            return(BadRequest(UserENUM.UNABLE_ADD_ROLE.ToString()));
        }