コード例 #1
0
        private async Task <UserLinkResult> InviteUserInternal(string sendingUserId, string connectUsername)
        {
            if (string.IsNullOrWhiteSpace(connectUsername))
            {
                throw new ArgumentNullException("connectUsername");
            }

            var connectUser = await GetConnectUser(new ConnectUserQuery
            {
                Name = connectUsername
            }, CancellationToken.None).ConfigureAwait(false);

            if (!connectUser.IsActive)
            {
                throw new ArgumentException("The Media Browser account has been disabled.");
            }

            var url = GetConnectUrl("ServerAuthorizations");

            var options = new HttpRequestOptions
            {
                Url = url,
                CancellationToken = CancellationToken.None
            };

            var accessToken = Guid.NewGuid().ToString("N");
            var sendingUser = GetUser(sendingUserId);

            var postData = new Dictionary <string, string>
            {
                { "serverId", ConnectServerId },
                { "userId", connectUser.Id },
                { "userType", "Guest" },
                { "accessToken", accessToken },
                { "requesterUserName", sendingUser.ConnectUserName }
            };

            options.SetPostData(postData);

            SetServerAccessToken(options);

            var result = new UserLinkResult();

            // No need to examine the response
            using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content)
            {
                var response = _json.DeserializeFromStream <ServerUserAuthorizationResponse>(stream);

                result.IsPending = string.Equals(response.AcceptStatus, "waiting", StringComparison.OrdinalIgnoreCase);
            }

            await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false);

            return(result);
        }
コード例 #2
0
        private async Task <UserLinkResult> InviteUserInternal(ConnectAuthorizationRequest request)
        {
            var connectUsername = request.ConnectUserName;
            var sendingUserId   = request.SendingUserId;

            if (string.IsNullOrWhiteSpace(connectUsername))
            {
                throw new ArgumentNullException("connectUsername");
            }
            if (string.IsNullOrWhiteSpace(ConnectServerId))
            {
                throw new ArgumentNullException("ConnectServerId");
            }

            var sendingUser       = GetUser(sendingUserId);
            var requesterUserName = sendingUser.ConnectUserName;

            if (string.IsNullOrWhiteSpace(requesterUserName))
            {
                throw new ArgumentException("A Connect account is required in order to send invitations.");
            }

            string connectUserId = null;
            var    result        = new UserLinkResult();

            try
            {
                var connectUser = await GetConnectUser(new ConnectUserQuery
                {
                    NameOrEmail = connectUsername
                }, CancellationToken.None).ConfigureAwait(false);

                if (!connectUser.IsActive)
                {
                    throw new ArgumentException("The Media Browser account has been disabled.");
                }

                connectUserId           = connectUser.Id;
                result.GuestDisplayName = connectUser.Name;
            }
            catch (HttpException ex)
            {
                if (!ex.StatusCode.HasValue ||
                    ex.StatusCode.Value != HttpStatusCode.NotFound ||
                    !Validator.EmailIsValid(connectUsername))
                {
                    throw;
                }
            }

            if (string.IsNullOrWhiteSpace(connectUserId))
            {
                return(await SendNewUserInvitation(requesterUserName, connectUsername).ConfigureAwait(false));
            }

            var url = GetConnectUrl("ServerAuthorizations");

            var options = new HttpRequestOptions
            {
                Url = url,
                CancellationToken = CancellationToken.None
            };

            var accessToken = Guid.NewGuid().ToString("N");

            var postData = new Dictionary <string, string>
            {
                { "serverId", ConnectServerId },
                { "userId", connectUserId },
                { "userType", "Guest" },
                { "accessToken", accessToken },
                { "requesterUserName", requesterUserName }
            };

            options.SetPostData(postData);

            SetServerAccessToken(options);
            SetApplicationHeader(options);

            // No need to examine the response
            using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content)
            {
                var response = _json.DeserializeFromStream <ServerUserAuthorizationResponse>(stream);

                result.IsPending = string.Equals(response.AcceptStatus, "waiting", StringComparison.OrdinalIgnoreCase);

                _data.PendingAuthorizations.Add(new ConnectAuthorizationInternal
                {
                    ConnectUserId     = response.UserId,
                    Id                = response.Id,
                    ImageUrl          = response.UserImageUrl,
                    UserName          = response.UserName,
                    ExcludedLibraries = request.ExcludedLibraries,
                    ExcludedChannels  = request.ExcludedChannels,
                    EnableLiveTv      = request.EnableLiveTv,
                    AccessToken       = accessToken
                });

                CacheData();
            }

            await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false);

            return(result);
        }
コード例 #3
0
        private async Task <UserLinkResult> LinkUserInternal(string userId, string connectUsername)
        {
            if (string.IsNullOrWhiteSpace(userId))
            {
                throw new ArgumentNullException("userId");
            }
            if (string.IsNullOrWhiteSpace(connectUsername))
            {
                throw new ArgumentNullException("connectUsername");
            }
            if (string.IsNullOrWhiteSpace(ConnectServerId))
            {
                throw new ArgumentNullException("ConnectServerId");
            }

            var connectUser = await GetConnectUser(new ConnectUserQuery
            {
                NameOrEmail = connectUsername
            }, CancellationToken.None).ConfigureAwait(false);

            if (!connectUser.IsActive)
            {
                throw new ArgumentException("The Media Browser account has been disabled.");
            }

            var user = GetUser(userId);

            if (!string.IsNullOrWhiteSpace(user.ConnectUserId))
            {
                await RemoveConnect(user, connectUser.Id).ConfigureAwait(false);
            }

            var url = GetConnectUrl("ServerAuthorizations");

            var options = new HttpRequestOptions
            {
                Url = url,
                CancellationToken = CancellationToken.None
            };

            var accessToken = Guid.NewGuid().ToString("N");

            var postData = new Dictionary <string, string>
            {
                { "serverId", ConnectServerId },
                { "userId", connectUser.Id },
                { "userType", "Linked" },
                { "accessToken", accessToken }
            };

            options.SetPostData(postData);

            SetServerAccessToken(options);
            SetApplicationHeader(options);

            var result = new UserLinkResult();

            // No need to examine the response
            using (var stream = (await _httpClient.Post(options).ConfigureAwait(false)).Content)
            {
                var response = _json.DeserializeFromStream <ServerUserAuthorizationResponse>(stream);

                result.IsPending = string.Equals(response.AcceptStatus, "waiting", StringComparison.OrdinalIgnoreCase);
            }

            user.ConnectAccessKey = accessToken;
            user.ConnectUserName  = connectUser.Name;
            user.ConnectUserId    = connectUser.Id;
            user.ConnectLinkType  = UserLinkType.LinkedUser;

            await user.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false);

            await _userManager.UpdateConfiguration(user.Id.ToString("N"), user.Configuration);

            await RefreshAuthorizationsInternal(false, CancellationToken.None).ConfigureAwait(false);

            return(result);
        }