예제 #1
0
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="update">代表更新的对象</param>
        /// <returns>状态码,<see cref="StatusCodes.Status500InternalServerError"/>代表出错,<see cref="StatusCodes.Status200OK"/>代表成功</returns>
        public async Task <int> UpdateUserDataAsync([NotNull] UserDataUpdate update)
        {
            // 尝试获取用户数据
            UserData data = await dataContext.FindAsync <UserData>(update.ID);

            if (data == null)
            {
                return(StatusCodes.Status404NotFound);
            }
            // 如果有邮箱或电话号码更新,发送请求
            if (update.Email != data.Email || update.PhoneNumber != data.PhoneNumber)
            {
                UserUpdateRequest request = new UserUpdateRequest();
                if (update.Email != null)
                {
                    request.Email = update.Email;
                }
                if (update.PhoneNumber != null)
                {
                    request.PhoneNumber = update.PhoneNumber;
                }

                request.Connection = configuration.GetAuth0Config(ConfigurationKeyConstants.Auth0Connection);

                if (null == await userClient.UpdateAsync(data.StringUserID, request))
                {
                    return(StatusCodes.Status500InternalServerError);
                }
            }
            // 如果有身份变化
            if (update.Role != null)
            {
                // 获取可用身份列表
                Role[] availableRoles = await roleGetter.GetAvailableRoles();

                // 去除重复项与不在身份列表内的项
                string[] UpdateRoles  = update.Role.Split(' ').Distinct().Where((name) => availableRoles.Any((role) => role.Name == name)).ToArray();
                string[] currentRoles = data.Role.Split(' ');
                update.Role = string.Join(' ', UpdateRoles);

                // 获取新增的身份
                string[] assignRoles = (from name in UpdateRoles
                                        where !currentRoles.Contains(name)
                                        // 确认包含于可用身份列表内
                                        let roleObject = availableRoles.FirstOrDefault((role) => role.Name == name)
                                                         where roleObject != null
                                                         // 选中ID
                                                         select roleObject.Id).ToArray();

                // 获取移除的身份
                string[] removeRoles = (from name in currentRoles
                                        where !UpdateRoles.Contains(name)
                                        // 确认包含于可用身份列表内
                                        let roleObject = availableRoles.FirstOrDefault((role) => role.Name == name)
                                        // 选中ID
                                                         where roleObject != null
                                                         select roleObject.Id).ToArray();

                // 如果有需要添加的身份
                if (assignRoles.Length > 0)
                {
                    // 添加身份
                    await userClient.AssignRolesAsync(data.StringUserID,
                                                      new AssignRolesRequest { Roles = assignRoles });
                }

                // 如果有需要移除的身份
                if (removeRoles.Length > 0)
                {
                    // 移除身份
                    await userClient.RemoveRolesAsync(data.StringUserID,
                                                      new AssignRolesRequest { Roles = removeRoles });
                }
            }

            // 应用变化然后保存
            data.ApplyUpdate(update);

            await dataContext.SaveWithExceptionHandlingAsync();

            return(StatusCodes.Status200OK);
        }