diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/Connect/ConnectManager.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Connect/ConnectManager.cs | 97 |
1 files changed, 56 insertions, 41 deletions
diff --git a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs index 972f3adb6..d92ca8323 100644 --- a/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs +++ b/MediaBrowser.Server.Implementations/Connect/ConnectManager.cs @@ -6,6 +6,7 @@ using MediaBrowser.Controller.Connect; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Security; +using MediaBrowser.Model.Connect; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; @@ -261,24 +262,12 @@ namespace MediaBrowser.Server.Implementations.Connect return user; } - public ConnectUserLink GetUserInfo(string userId) - { - var user = GetUser(userId); - - return new ConnectUserLink - { - LocalUserId = user.Id.ToString("N"), - Username = user.ConnectUserName, - UserId = user.ConnectUserId - }; - } - private string GetConnectUrl(string handler) { return "https://connect.mediabrowser.tv/service/" + handler; } - public async Task LinkUser(string userId, string connectUsername) + public async Task<UserLinkResult> LinkUser(string userId, string connectUsername) { if (string.IsNullOrWhiteSpace(connectUsername)) { @@ -291,11 +280,16 @@ namespace MediaBrowser.Server.Implementations.Connect }, 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 RemoveLink(user, connectUser).ConfigureAwait(false); + await RemoveLink(user, connectUser.Id).ConfigureAwait(false); } var url = GetConnectUrl("ServerAuthorizations"); @@ -320,59 +314,79 @@ namespace MediaBrowser.Server.Implementations.Connect 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); } user.ConnectAccessKey = accessToken; user.ConnectUserName = connectUser.Name; user.ConnectUserId = connectUser.Id; + user.ConnectLinkType = UserLinkType.LinkedUser; await user.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false); + + return result; } - public async Task RemoveLink(string userId) + public Task RemoveLink(string userId) { var user = GetUser(userId); - var connectUser = await GetConnectUser(new ConnectUserQuery - { - Name = user.ConnectUserId - - }, CancellationToken.None).ConfigureAwait(false); - - await RemoveLink(user, connectUser).ConfigureAwait(false); + return RemoveLink(user, user.ConnectUserId); } - public async Task RemoveLink(User user, ConnectUser connectUser) + private async Task RemoveLink(User user, string connectUserId) { - var url = GetConnectUrl("ServerAuthorizations"); - - var options = new HttpRequestOptions + if (!string.IsNullOrWhiteSpace(connectUserId)) { - Url = url, - CancellationToken = CancellationToken.None - }; + var url = GetConnectUrl("ServerAuthorizations"); - var postData = new Dictionary<string, string> - { - {"serverId", ConnectServerId}, - {"userId", connectUser.Id} - }; + var options = new HttpRequestOptions + { + Url = url, + CancellationToken = CancellationToken.None + }; - options.SetPostData(postData); + var postData = new Dictionary<string, string> + { + {"serverId", ConnectServerId}, + {"userId", connectUserId} + }; - SetServerAccessToken(options); + options.SetPostData(postData); - // No need to examine the response - using (var stream = (await _httpClient.SendAsync(options, "DELETE").ConfigureAwait(false)).Content) - { + SetServerAccessToken(options); + + try + { + // No need to examine the response + using (var stream = (await _httpClient.SendAsync(options, "DELETE").ConfigureAwait(false)).Content) + { + } + } + catch (HttpException ex) + { + // If connect says the auth doesn't exist, we can handle that gracefully since this is a remove operation + + if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound) + { + throw; + } + + _logger.Debug("Connect returned a 404 when removing a user auth link. Handling it."); + } } - + user.ConnectAccessKey = null; user.ConnectUserName = null; user.ConnectUserId = null; + user.ConnectLinkType = UserLinkType.LinkedUser; await user.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false); } @@ -410,7 +424,8 @@ namespace MediaBrowser.Server.Implementations.Connect { Email = response.Email, Id = response.Id, - Name = response.Name + Name = response.Name, + IsActive = response.IsActive }; } } |
