Skip to content

Commit

Permalink
Merge pull request #39 from shiitake/dev
Browse files Browse the repository at this point in the history
Fixes for Checking if the user is the owner
  • Loading branch information
Jamie committed Mar 19, 2016
2 parents 8b76732 + c28a264 commit c5db1ed
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
2 changes: 1 addition & 1 deletion PlexRequests.Api.Interfaces/IPlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public interface IPlexApi
PlexFriends GetUsers(string authToken);
PlexSearch SearchContent(string authToken, string searchTerm, Uri plexFullHost);
PlexStatus GetStatus(string authToken, Uri uri);

PlexAccount GetAccount(string authToken);
}
}
22 changes: 22 additions & 0 deletions PlexRequests.Api.Models/Plex/PlexAccount.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace PlexRequests.Api.Models.Plex
{
[XmlRoot(ElementName = "user")]
public class PlexAccount
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "username")]
public string Username { get; set; }
[XmlAttribute(AttributeName = "email")]
public string Email { get; set; }
[XmlAttribute(AttributeName = "authenticationToken")]
public string AuthToken { get; set; }
}
}
1 change: 1 addition & 0 deletions PlexRequests.Api.Models/PlexRequests.Api.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Movie\CouchPotatoStatus.cs" />
<Compile Include="Notifications\PushbulletPush.cs" />
<Compile Include="Notifications\PushbulletResponse.cs" />
<Compile Include="Plex\PlexAccount.cs" />
<Compile Include="Plex\PlexAuthentication.cs" />
<Compile Include="Plex\PlexError.cs" />
<Compile Include="Plex\PlexFriends.cs" />
Expand Down
19 changes: 19 additions & 0 deletions PlexRequests.Api/PlexApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ public PlexStatus GetStatus(string authToken, Uri uri)

return users;
}

public PlexAccount GetAccount(string authToken)
{
var request = new RestRequest
{
Method = Method.GET,
};

request.AddHeader("X-Plex-Client-Identifier", "Test213");
request.AddHeader("X-Plex-Product", "Request Plex");
request.AddHeader("X-Plex-Version", Version);
request.AddHeader("X-Plex-Token", authToken);
request.AddHeader("Content-Type", "application/xml");

var api = new ApiRequest();
var account = api.ExecuteXml<PlexAccount>(request, new Uri("https://plex.tv/users/account"));

return account;
}
}
}

21 changes: 21 additions & 0 deletions PlexRequests.Store/SqlTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ CREATE TABLE IF NOT EXISTS Log
Message varchar(100) NOT NULL,
CallSite varchar(100) NOT NULL,
Exception varchar(100) NOT NULL
);

CREATE TABLE IF NOT EXISTS Requested
(
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Type INTEGER NOT NULL,
ProviderId INTEGER NOT NULL,
ImdbId varchar(50),
Overview varchar(50),
Title varchar(50) NOT NULL,
PosterPath varchar(50) NOT NULL,
ReleaseDate varchar(50) NOT NULL,
Status varchar(50) NOT NULL,
AdminNote varchar(50),
Approved INTEGER NOT NULL,
LatestTv INTEGER NOT NULL,
RequestedBy varchar(50),
RequestedDate varchar(50) NOT NULL,
Available INTEGER(50),
Issues INTEGER,
OtherMessage varchar(50)
);
24 changes: 21 additions & 3 deletions PlexRequests.UI/Modules/UserLoginModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using System.Collections.Generic;
using System.Linq;

using Nancy;
Expand Down Expand Up @@ -98,9 +100,17 @@ private Response LoginUser()
var signedIn = (PlexAuthentication)Api.SignIn(username, password);
if (signedIn.user?.authentication_token != null)
{
Log.Debug("Correct credentials, checking if the user is in the friends list");
authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken);
Log.Debug("Friends list result = {0}", authenticated);
Log.Debug("Correct credentials, checking if the user is account owner or in the friends list");
if (CheckIfUserIsOwner(settings.PlexAuthToken, username))
{
Log.Debug("User is the account owner");
authenticated = true;
}
else
{
authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken);
Log.Debug("Friends list result = {0}", authenticated);
}
}
}
else if(settings.UserAuthentication) // Check against the users in Plex
Expand All @@ -127,6 +137,8 @@ private Response LoginUser()
: new JsonResponseModel { Result = false, Message = "Incorrect User or Password"});
}



private Response Logout()
{
Log.Debug("Logging Out");
Expand All @@ -137,6 +149,12 @@ private Response Logout()
return Context.GetRedirect("~/userlogin");
}

private bool CheckIfUserIsOwner(string authToken, string userName)
{
var userAccount = Api.GetAccount(authToken);
return userAccount.Username == userName;
}

private bool CheckIfUserIsInPlexFriends(string username, string authToken)
{
var users = Api.GetUsers(authToken);
Expand Down

0 comments on commit c5db1ed

Please sign in to comment.