Exemplo n.º 1
0
        private AuthBasicUserInfoContract GetBasicUserFromAuthService(int userExternalId)
        {
            var client = m_communicationProvider.GetAuthUserApiClient();
            var result = client.GetBasicUserInfoAsync(UserIdentifierTypeContract.DatabaseId, userExternalId.ToString()).GetAwaiter().GetResult();

            return(result);
        }
Exemplo n.º 2
0
        public int CreateUserIfNotExist(CreateUserIfNotExistContract data)
        {
            var userExternalId = data.ExternalId;
            var userInfo       = new UpdateUserInfo(data.Username, data.FirstName, data.LastName);

            var authUserApiClient = m_communicationProvider.GetAuthUserApiClient();
            var userRoles         = authUserApiClient.GetRolesByUserAsync(userExternalId).GetAwaiter().GetResult();
            var userId            = new CreateOrUpdateUserIfNotExistWork(m_userRepository, userExternalId, userRoles, userInfo, m_codeGenerator).Execute();

            return(userId);
        }
Exemplo n.º 3
0
        protected override void ExecuteWorkImplementation()
        {
            var group     = m_permissionRepository.FindById <UserGroup>(m_roleId);
            var roleGroup = group as RoleUserGroup;
            var role      = m_defaultUserProvider.GetDefaultUnregisteredRole();

            if (roleGroup != null && role.Id == roleGroup.ExternalId)
            {
                throw new MainServiceException(MainServiceErrorCode.AddUserToDefaultRole,
                                               $"Users cannot be added to the default role {role.Name}",
                                               HttpStatusCode.BadRequest,
                                               role.Name
                                               );
            }

            var user = m_permissionRepository.GetUserWithGroups(m_userId);

            if (user.ExternalId == null)
            {
                throw new MainServiceException(MainServiceErrorCode.UserHasMissingExternalId,
                                               $"User with ID {user.Id} has missing ExternalID",
                                               HttpStatusCode.BadRequest,
                                               user.Id
                                               );
            }

            if (user.Groups == null)
            {
                user.Groups = new List <UserGroup>();
            }

            if (user.Groups.Contains(group))
            {
                throw new MainServiceException(MainServiceErrorCode.UserAlreadyAssignedToRole,
                                               $"User with ID={user.Id} is already assigned to group with ID={group.Id}",
                                               HttpStatusCode.BadRequest,
                                               user.ExtUsername, group.Name
                                               );
            }
            // Assign group to user (fetch lower amount of data)

            user.Groups.Add(group);
            m_permissionRepository.Save(user);
            m_permissionRepository.Flush();


            if (roleGroup != null)
            {
                var client = m_communicationProvider.GetAuthUserApiClient();
                client.AddRoleToUserAsync(user.ExternalId.Value, roleGroup.ExternalId).GetAwaiter().GetResult();
            }
            else
            {
                throw new InvalidOperationException($"Only RoleUserGroup can be updated by this method, argument type was: {group.GetType()}");
            }
        }
Exemplo n.º 4
0
        public List <UserGroupContract> GetUserGroupsByUser(int userId)
        {
            var user = m_userRepository.InvokeUnitOfWork(x => x.GetUserById(userId));

            if (user == null)
            {
                var message = $"Cannot locate user with id '{userId}'";
                if (m_log.IsErrorEnabled)
                {
                    m_log.Error(message);
                }

                throw new MainServiceException(MainServiceErrorCode.CannotLocateUser,
                                               message,
                                               HttpStatusCode.BadRequest,
                                               new object[] { userId }
                                               );
            }

            if (user.ExternalId == null)
            {
                throw new MainServiceException(MainServiceErrorCode.UserHasMissingExternalId,
                                               $"User with ID {user.Id} has missing ExternalID",
                                               HttpStatusCode.BadRequest,
                                               new object[] { user.Id }
                                               );
            }

            var client = m_communicationProvider.GetAuthUserApiClient();

            var authUser = client.GetUserForRoleAssignmentAsync(user.ExternalId.Value).GetAwaiter().GetResult();

            new GetOrCreateUserGroupsWork <AuthRoleContractBase>(m_userRepository, authUser.Roles, userId).Execute();

            var dbGroups   = m_permissionRepository.InvokeUnitOfWork(x => x.GetUserGroupsByUser(userId));
            var resultList = m_mapper.Map <List <UserGroupContract> >(dbGroups);

            // Fill description for RoleUserGroups (local database doesn't contain this)
            foreach (var roleUserGroup in resultList.Where(x => x.Type == UserGroupTypeContract.Role))
            {
                roleUserGroup.Description = authUser.Roles.First(x => x.Id == roleUserGroup.ExternalId).Description;
            }

            return(resultList);
        }
Exemplo n.º 5
0
        protected override void ExecuteWorkImplementation()
        {
            var group = m_permissionRepository.FindById <UserGroup>(m_roleId);
            var user  = m_permissionRepository.GetUserWithGroups(m_userId);

            if (user.ExternalId == null)
            {
                throw new MainServiceException(MainServiceErrorCode.UserHasMissingExternalId,
                                               $"User with ID {user.Id} has missing ExternalID",
                                               HttpStatusCode.BadRequest,
                                               new object[] { user.Id }
                                               );
            }

            if (user.Groups == null)
            {
                if (m_log.IsWarnEnabled)
                {
                    string message = $"Cannot remove Group with ID '{group.Id}' from User with ID '{user.Id}'. User is empty.";
                    m_log.Warn(message);
                }

                return;
            }

            // Remove group from user (fetch lower amount of data)

            user.Groups.Remove(group);
            m_permissionRepository.Save(user);
            m_permissionRepository.Flush();


            if (group is RoleUserGroup roleUserGroup)
            {
                var client = m_communicationProvider.GetAuthUserApiClient();
                client.RemoveRoleFromUserAsync(user.ExternalId.Value, roleUserGroup.ExternalId).GetAwaiter().GetResult();
            }
            else
            {
                throw new InvalidOperationException($"Only RoleUserGroup can be updated by this method, argument type was: {group.GetType()}");
            }
        }