User/Roles for webserver
This commit is contained in:
1 parent
c41af99536
commit
3126d5aaf0
13 files changed
+564
-34
No files matched your search
@@ -94,6 +94,45 @@ namespace ServerLib
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region user management
|
||||||
|
|
||||||
|
private static SetPasswordDelegate setPassword;
|
||||||
|
public static SetPasswordDelegate SetPassword
|
||||||
|
{
|
||||||
|
internal get { return CServer.setPassword; }
|
||||||
|
set { CServer.setPassword = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ValidatePasswordDelegate validatePassword;
|
||||||
|
public static ValidatePasswordDelegate ValidatePassword
|
||||||
|
{
|
||||||
|
internal get { return CServer.validatePassword; }
|
||||||
|
set { CServer.validatePassword = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static GetUserRoleDelegate getUserRole;
|
||||||
|
public static GetUserRoleDelegate GetUserRole
|
||||||
|
{
|
||||||
|
internal get { return CServer.getUserRole; }
|
||||||
|
set { CServer.getUserRole = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SetUserRoleDelegate setUserRole;
|
||||||
|
public static SetUserRoleDelegate SetUserRole
|
||||||
|
{
|
||||||
|
internal get { return CServer.setUserRole; }
|
||||||
|
set { CServer.setUserRole = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static GetUserIdFromUsernameDelegate getUserIdFromUsername;
|
||||||
|
public static GetUserIdFromUsernameDelegate GetUserIdFromUsername
|
||||||
|
{
|
||||||
|
internal get { return CServer.getUserIdFromUsername; }
|
||||||
|
set { CServer.getUserIdFromUsername = value; }
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public CServer(int port, bool encrypted)
|
public CServer(int port, bool encrypted)
|
||||||
@@ -101,6 +140,8 @@ namespace ServerLib
|
|||||||
init(port, encrypted);
|
init(port, encrypted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region server control
|
||||||
|
|
||||||
private void init(int port, bool encrypted)
|
private void init(int port, bool encrypted)
|
||||||
{
|
{
|
||||||
string hostname = Dns.GetHostName();
|
string hostname = Dns.GetHostName();
|
||||||
@@ -178,7 +219,7 @@ namespace ServerLib
|
|||||||
|
|
||||||
private void registerUrlAndCert(int port)
|
private void registerUrlAndCert(int port)
|
||||||
{
|
{
|
||||||
#if WIN
|
#if WIN
|
||||||
|
|
||||||
ProcessStartInfo info = new ProcessStartInfo();
|
ProcessStartInfo info = new ProcessStartInfo();
|
||||||
info.FileName = "WebserverInitalConfig.exe";
|
info.FileName = "WebserverInitalConfig.exe";
|
||||||
@@ -194,11 +235,14 @@ namespace ServerLib
|
|||||||
p.Close();
|
p.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
//Required?
|
//Required?
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ using System.Text;
|
|||||||
using System.ServiceModel.Web;
|
using System.ServiceModel.Web;
|
||||||
using System.Web.Services;
|
using System.Web.Services;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.ServiceModel.Channels;
|
||||||
|
|
||||||
namespace ServerLib
|
namespace ServerLib
|
||||||
{
|
{
|
||||||
@@ -20,6 +21,12 @@ namespace ServerLib
|
|||||||
|
|
||||||
#region profile
|
#region profile
|
||||||
|
|
||||||
|
[OperationContract]
|
||||||
|
[WebInvoke(Method = "GET",
|
||||||
|
ResponseFormat = WebMessageFormat.Json,
|
||||||
|
UriTemplate = "/getOwnProfileId")]
|
||||||
|
int getOwnProfileId();
|
||||||
|
|
||||||
[OperationContract]
|
[OperationContract]
|
||||||
[WebInvoke(Method = "POST",
|
[WebInvoke(Method = "POST",
|
||||||
ResponseFormat = WebMessageFormat.Json,
|
ResponseFormat = WebMessageFormat.Json,
|
||||||
@@ -53,6 +60,12 @@ namespace ServerLib
|
|||||||
|
|
||||||
#region website
|
#region website
|
||||||
|
|
||||||
|
[OperationContract]
|
||||||
|
[WebInvoke(Method = "GET",
|
||||||
|
ResponseFormat = WebMessageFormat.Json,
|
||||||
|
UriTemplate = "/login?username={username}&password={password}")]
|
||||||
|
Guid login(string username, string password);
|
||||||
|
|
||||||
[OperationContract]
|
[OperationContract]
|
||||||
[WebInvoke(Method = "GET",
|
[WebInvoke(Method = "GET",
|
||||||
ResponseFormat = WebMessageFormat.Json,
|
ResponseFormat = WebMessageFormat.Json,
|
||||||
@@ -114,8 +127,35 @@ namespace ServerLib
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Guid getSession()
|
||||||
|
{
|
||||||
|
Guid sessionKey = Guid.Empty;
|
||||||
|
string sessionHeader =
|
||||||
|
((HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties["httpRequest"]).Headers["session"];
|
||||||
|
if (sessionHeader == null || sessionHeader == "")
|
||||||
|
{
|
||||||
|
return sessionKey;
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
sessionKey = Guid.Parse(sessionHeader);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return sessionKey;
|
||||||
|
}
|
||||||
|
|
||||||
public bool sendKeyEvent(string key)
|
public bool sendKeyEvent(string key)
|
||||||
{
|
{
|
||||||
|
Guid sessionKey = getSession();
|
||||||
|
|
||||||
|
if (sessionKey == Guid.Empty || !SessionControl.requestRight(sessionKey, UserRights.UseKeyboard))
|
||||||
|
{
|
||||||
|
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (CServer.SendKeyEvent == null)
|
if (CServer.SendKeyEvent == null)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -125,22 +165,58 @@ namespace ServerLib
|
|||||||
|
|
||||||
#region profile
|
#region profile
|
||||||
|
|
||||||
|
public int getOwnProfileId()
|
||||||
|
{
|
||||||
|
Guid sessionKey = getSession();
|
||||||
|
if (sessionKey == Guid.Empty)
|
||||||
|
{
|
||||||
|
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SessionControl.getUserIdFromSession(sessionKey);
|
||||||
|
}
|
||||||
|
|
||||||
public bool sendProfile(ProfileData profile)
|
public bool sendProfile(ProfileData profile)
|
||||||
{
|
{
|
||||||
if (CServer.SendProfileData == null)
|
Guid sessionKey = getSession();
|
||||||
|
|
||||||
|
if (sessionKey == Guid.Empty || (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles) &&
|
||||||
|
SessionControl.getUserIdFromSession(sessionKey) != profile.ProfileId))
|
||||||
{
|
{
|
||||||
|
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (CServer.SendProfileData == null)
|
||||||
|
{
|
||||||
|
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return CServer.SendProfileData(profile);
|
return CServer.SendProfileData(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProfileData getProfile(int profileId)
|
public ProfileData getProfile(int profileId)
|
||||||
{
|
{
|
||||||
|
Guid sessionKey = getSession();
|
||||||
|
|
||||||
|
if (sessionKey == Guid.Empty || (!SessionControl.requestRight(sessionKey, UserRights.ViewOtherProfiles) &&
|
||||||
|
SessionControl.getUserIdFromSession(sessionKey) != profileId))
|
||||||
|
{
|
||||||
|
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
|
||||||
|
return new ProfileData();
|
||||||
|
}
|
||||||
|
|
||||||
if (CServer.GetProfileData == null)
|
if (CServer.GetProfileData == null)
|
||||||
{
|
{
|
||||||
return new ProfileData();
|
return new ProfileData();
|
||||||
}
|
}
|
||||||
return CServer.GetProfileData(profileId);
|
|
||||||
|
bool isReadonly = (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles) &&
|
||||||
|
SessionControl.getUserIdFromSession(sessionKey) != profileId);
|
||||||
|
|
||||||
|
return CServer.GetProfileData(profileId, isReadonly);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProfileData[] getProfileList()
|
public ProfileData[] getProfileList()
|
||||||
@@ -158,6 +234,13 @@ namespace ServerLib
|
|||||||
|
|
||||||
public bool sendPhoto(PhotoData photo)
|
public bool sendPhoto(PhotoData photo)
|
||||||
{
|
{
|
||||||
|
Guid sessionKey = getSession();
|
||||||
|
if (!SessionControl.requestRight(sessionKey, UserRights.UploadPhotos))
|
||||||
|
{
|
||||||
|
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return CServer.SendPhoto(photo);
|
return CServer.SendPhoto(photo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +248,16 @@ namespace ServerLib
|
|||||||
|
|
||||||
#region website
|
#region website
|
||||||
|
|
||||||
|
public Guid login(string username, string password)
|
||||||
|
{
|
||||||
|
Guid sessionId = SessionControl.openSession(username, password);
|
||||||
|
if (sessionId == Guid.Empty)
|
||||||
|
{
|
||||||
|
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
|
||||||
|
}
|
||||||
|
return sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
public Stream Index()
|
public Stream Index()
|
||||||
{
|
{
|
||||||
if (WebOperationContext.Current != null)
|
if (WebOperationContext.Current != null)
|
||||||
@@ -277,6 +370,5 @@ namespace ServerLib
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ namespace ServerLib
|
|||||||
|
|
||||||
#region profile
|
#region profile
|
||||||
|
|
||||||
public delegate ProfileData GetProfileDataDelegate(int profileId);
|
public delegate ProfileData GetProfileDataDelegate(int profileId, bool isReadonly);
|
||||||
public delegate bool SendProfileDataDelegate(ProfileData profile);
|
public delegate bool SendProfileDataDelegate(ProfileData profile);
|
||||||
public delegate ProfileData[] GetProfileListDelegate();
|
public delegate ProfileData[] GetProfileListDelegate();
|
||||||
|
|
||||||
@@ -121,4 +121,18 @@ namespace ServerLib
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region user management
|
||||||
|
|
||||||
|
public delegate void SetPasswordDelegate(int profileId, string newPassword);
|
||||||
|
|
||||||
|
public delegate bool ValidatePasswordDelegate(int profileId, string password);
|
||||||
|
|
||||||
|
public delegate int GetUserRoleDelegate(int profileId);
|
||||||
|
|
||||||
|
public delegate void SetUserRoleDelegate(int profileId, int userRole);
|
||||||
|
|
||||||
|
public delegate int GetUserIdFromUsernameDelegate(string username);
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
@@ -52,6 +52,11 @@
|
|||||||
<Compile Include="CWebservice.cs" />
|
<Compile Include="CWebservice.cs" />
|
||||||
<Compile Include="DataStructs.cs" />
|
<Compile Include="DataStructs.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="Session.cs" />
|
||||||
|
<Compile Include="SessionControl.cs" />
|
||||||
|
<Compile Include="UserRights.cs" />
|
||||||
|
<Compile Include="UserRoleControl.cs" />
|
||||||
|
<Compile Include="UserRoles.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ServerLib
|
||||||
|
{
|
||||||
|
internal class Session
|
||||||
|
{
|
||||||
|
Guid id;
|
||||||
|
public Guid Id
|
||||||
|
{
|
||||||
|
get { return id; }
|
||||||
|
}
|
||||||
|
|
||||||
|
int profileId;
|
||||||
|
public int ProfileId
|
||||||
|
{
|
||||||
|
get { return profileId; }
|
||||||
|
}
|
||||||
|
|
||||||
|
UserRoles roles;
|
||||||
|
internal UserRoles Roles
|
||||||
|
{
|
||||||
|
get { return roles; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public Session(Guid id, int profileId, UserRoles roles)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
this.profileId = profileId;
|
||||||
|
this.roles = roles;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ServerLib
|
||||||
|
{
|
||||||
|
internal static class SessionControl
|
||||||
|
{
|
||||||
|
private static Dictionary<Guid, Session> activeSessions = new Dictionary<Guid, Session>();
|
||||||
|
|
||||||
|
public static Guid openSession(string userName, string password)
|
||||||
|
{
|
||||||
|
if (!validateUserAndPassword(userName, password))
|
||||||
|
{
|
||||||
|
return Guid.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
Guid newId = Guid.NewGuid();
|
||||||
|
int id = getProfileIdFormUsername(userName);
|
||||||
|
UserRoles roles = getUserRoles(id);
|
||||||
|
Session session = new Session(newId, id, roles);
|
||||||
|
invalidateSessions(id);
|
||||||
|
activeSessions.Add(newId, session);
|
||||||
|
|
||||||
|
return newId;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool validateUserAndPassword(string userName, string password)
|
||||||
|
{
|
||||||
|
return CServer.ValidatePassword(getProfileIdFormUsername(userName), password);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getProfileIdFormUsername(string username)
|
||||||
|
{
|
||||||
|
return CServer.GetUserIdFromUsername(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static UserRoles getUserRoles(int profileId)
|
||||||
|
{
|
||||||
|
return (UserRoles)CServer.GetUserRole(profileId);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static void invalidateSessions(int profileId)
|
||||||
|
{
|
||||||
|
foreach (var s in (from kv in activeSessions
|
||||||
|
where kv.Value.ProfileId == profileId
|
||||||
|
select kv).ToList())
|
||||||
|
{
|
||||||
|
activeSessions.Remove(s.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static bool requestRight(Guid sessionId, UserRights requestedRight)
|
||||||
|
{
|
||||||
|
return ((UserRoleControl.getUserRightsFromUserRole(activeSessions[sessionId].Roles) & requestedRight) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static int getUserIdFromSession(Guid sessionId)
|
||||||
|
{
|
||||||
|
return activeSessions[sessionId].ProfileId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ServerLib
|
||||||
|
{
|
||||||
|
[Flags]
|
||||||
|
internal enum UserRights
|
||||||
|
{
|
||||||
|
None = 0x00,
|
||||||
|
EditAllProfiles = 0x01,
|
||||||
|
UploadPhotos = 0x02,
|
||||||
|
ViewOtherProfiles = 0x04,
|
||||||
|
UseKeyboard = 0x08
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ServerLib
|
||||||
|
{
|
||||||
|
internal static class UserRoleControl
|
||||||
|
{
|
||||||
|
private static Dictionary<UserRoles, UserRights> roleRightsMapping = new Dictionary<UserRoles, UserRights> {
|
||||||
|
{UserRoles.Administrator,
|
||||||
|
(UserRights.EditAllProfiles|
|
||||||
|
UserRights.UploadPhotos|
|
||||||
|
UserRights.ViewOtherProfiles)|
|
||||||
|
UserRights.UseKeyboard},
|
||||||
|
{UserRoles.AuthenticatedUser,
|
||||||
|
(UserRights.UploadPhotos|
|
||||||
|
UserRights.ViewOtherProfiles|
|
||||||
|
UserRights.UseKeyboard)}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
internal static UserRights getUserRightsFromUserRole(UserRoles userRole)
|
||||||
|
{
|
||||||
|
UserRights resultRights = UserRights.None;
|
||||||
|
|
||||||
|
foreach (UserRights r in roleRightsMapping.Where(role => userRole.HasFlag(role.Key)).Select(role => role.Value))
|
||||||
|
{
|
||||||
|
resultRights |= r;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultRights;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace ServerLib
|
||||||
|
{
|
||||||
|
[Flags]
|
||||||
|
internal enum UserRoles
|
||||||
|
{
|
||||||
|
AuthenticatedUser = 0x00,
|
||||||
|
Administrator = 0x01
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,6 +29,7 @@ using Vocaluxe.Lib.Input;
|
|||||||
using VocaluxeLib;
|
using VocaluxeLib;
|
||||||
using VocaluxeLib.Profile;
|
using VocaluxeLib.Profile;
|
||||||
using VocaluxeLib.Songs;
|
using VocaluxeLib.Songs;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace Vocaluxe.Base.Server
|
namespace Vocaluxe.Base.Server
|
||||||
{
|
{
|
||||||
@@ -44,7 +45,7 @@ namespace Vocaluxe.Base.Server
|
|||||||
{
|
{
|
||||||
_Clients = new Dictionary<int, CClientHandler>();
|
_Clients = new Dictionary<int, CClientHandler>();
|
||||||
|
|
||||||
_Server = new CServer(CConfig.ServerPort, CConfig.ServerEncryption == EOffOn.TR_CONFIG_ON);
|
_Server = new CServer(CConfig.ServerPort, CConfig.ServerEncryption == EOffOn.TR_CONFIG_ON);
|
||||||
//_Server = new CServer(RequestHandler, CConfig.ServerPort, CConfig.ServerPassword);
|
//_Server = new CServer(RequestHandler, CConfig.ServerPort, CConfig.ServerPassword);
|
||||||
|
|
||||||
CServer.SendKeyEvent = sendKeyEvent;
|
CServer.SendKeyEvent = sendKeyEvent;
|
||||||
@@ -56,6 +57,11 @@ namespace Vocaluxe.Base.Server
|
|||||||
CServer.GetSong = getSong;
|
CServer.GetSong = getSong;
|
||||||
CServer.GetAllSongs = getAllSongs;
|
CServer.GetAllSongs = getAllSongs;
|
||||||
CServer.GetCurrentSongId = getCurrentSongId;
|
CServer.GetCurrentSongId = getCurrentSongId;
|
||||||
|
CServer.SetPassword = setPassword;
|
||||||
|
CServer.ValidatePassword = validatePassword;
|
||||||
|
CServer.GetUserRole = getUserRole;
|
||||||
|
CServer.SetUserRole = setUserRole;
|
||||||
|
CServer.GetUserIdFromUsername = getUserIdFromUsername;
|
||||||
|
|
||||||
_Discover = new CDiscover(CConfig.ServerPort, CCommands.BroadcastKeyword);
|
_Discover = new CDiscover(CConfig.ServerPort, CCommands.BroadcastKeyword);
|
||||||
Controller.Init();
|
Controller.Init();
|
||||||
@@ -148,14 +154,14 @@ namespace Vocaluxe.Base.Server
|
|||||||
|
|
||||||
#region profile
|
#region profile
|
||||||
|
|
||||||
private static ProfileData getProfileData(int profileId)
|
private static ProfileData getProfileData(int profileId, bool isReadonly)
|
||||||
{
|
{
|
||||||
CProfile profile = CProfiles.GetProfile(profileId);
|
CProfile profile = CProfiles.GetProfile(profileId);
|
||||||
if (profile == null)
|
if (profile == null)
|
||||||
{
|
{
|
||||||
return new ProfileData();
|
return new ProfileData();
|
||||||
}
|
}
|
||||||
return CreateProfileData(profile); ;
|
return CreateProfileData(profile, isReadonly); ;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool sendProfileData(ProfileData profile)
|
private static bool sendProfileData(ProfileData profile)
|
||||||
@@ -226,17 +232,17 @@ namespace Vocaluxe.Base.Server
|
|||||||
|
|
||||||
foreach (CProfile profile in CProfiles.GetProfiles())
|
foreach (CProfile profile in CProfiles.GetProfiles())
|
||||||
{
|
{
|
||||||
result.Add(CreateProfileData(profile));
|
result.Add(CreateProfileData(profile, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.ToArray();
|
return result.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ProfileData CreateProfileData(CProfile profile)
|
private static ProfileData CreateProfileData(CProfile profile, bool isReadonly)
|
||||||
{
|
{
|
||||||
ProfileData profileData = new ProfileData();
|
ProfileData profileData = new ProfileData();
|
||||||
|
|
||||||
profileData.IsEditable = true; //TODO: set correctly
|
profileData.IsEditable = !isReadonly;
|
||||||
profileData.ProfileId = profile.ID;
|
profileData.ProfileId = profile.ID;
|
||||||
profileData.PlayerName = profile.PlayerName;
|
profileData.PlayerName = profile.PlayerName;
|
||||||
profileData.Type = (int)profile.GuestProfile;
|
profileData.Type = (int)profile.GuestProfile;
|
||||||
@@ -369,6 +375,141 @@ namespace Vocaluxe.Base.Server
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region user management
|
||||||
|
|
||||||
|
private static void setPassword(int profileId, string newPassword)
|
||||||
|
{
|
||||||
|
CProfile profile = CProfiles.GetProfile(profileId);
|
||||||
|
if (profile == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
|
||||||
|
byte[] buffer = new byte[128];
|
||||||
|
rng.GetNonZeroBytes(buffer);
|
||||||
|
byte[] salt = buffer;
|
||||||
|
byte[] hashedPassword = hash((new System.Text.UTF8Encoding()).GetBytes(newPassword), salt);
|
||||||
|
|
||||||
|
profile.PasswordSalt = salt;
|
||||||
|
profile.PasswordHash = hashedPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool validatePassword(int profileId, string password)
|
||||||
|
{
|
||||||
|
CProfile profile = CProfiles.GetProfile(profileId);
|
||||||
|
if (profile == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (profile.PasswordHash == null)
|
||||||
|
{
|
||||||
|
if (password == "" || password == null)
|
||||||
|
{
|
||||||
|
return true; //Allow emty passwords
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] salt = profile.PasswordSalt;
|
||||||
|
return hash((new System.Text.UTF8Encoding()).GetBytes(password), salt).SequenceEqual(profile.PasswordHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool validatePassword(int profileId, byte[] hashedPassword)
|
||||||
|
{
|
||||||
|
CProfile profile = CProfiles.GetProfile(profileId);
|
||||||
|
if (profile == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (profile.PasswordHash == null)
|
||||||
|
{
|
||||||
|
if (hashedPassword == null)
|
||||||
|
{
|
||||||
|
return true; //Allow emty passwords
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] salt = profile.PasswordSalt;
|
||||||
|
return hashedPassword.SequenceEqual(profile.PasswordHash);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] getPassowordSalt(int profileId)
|
||||||
|
{
|
||||||
|
CProfile profile = CProfiles.GetProfile(profileId);
|
||||||
|
if (profile == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (profile.PasswordHash == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Emty password");
|
||||||
|
}
|
||||||
|
|
||||||
|
return profile.PasswordSalt;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getUserRole(int profileId)
|
||||||
|
{
|
||||||
|
CProfile profile = CProfiles.GetProfile(profileId);
|
||||||
|
if (profile == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
return profile.UserRoles;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setUserRole(int profileId, int userRole)
|
||||||
|
{
|
||||||
|
CProfile profile = CProfiles.GetProfile(profileId);
|
||||||
|
if (profile == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid profileId");
|
||||||
|
}
|
||||||
|
|
||||||
|
profile.UserRoles = userRole;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int getUserIdFromUsername(string username)
|
||||||
|
{
|
||||||
|
var playerIds = (from p in CProfiles.GetProfiles()
|
||||||
|
where String.Equals(p.PlayerName, username,StringComparison.OrdinalIgnoreCase)
|
||||||
|
select p.ID);
|
||||||
|
if (playerIds.Count() == 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid playername");
|
||||||
|
}
|
||||||
|
|
||||||
|
return playerIds.First();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] hash(byte[] password, byte[] salt)
|
||||||
|
{
|
||||||
|
HashAlgorithm hashAlgo = new SHA256Managed();
|
||||||
|
|
||||||
|
byte[] data = new byte[password.Length + salt.Length];
|
||||||
|
|
||||||
|
password.CopyTo(data, 0);
|
||||||
|
salt.CopyTo(data, password.Length);
|
||||||
|
|
||||||
|
return hashAlgo.ComputeHash(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static string _SaveImage(Base64Image imageDate, string name, string folder)
|
private static string _SaveImage(Base64Image imageDate, string name, string folder)
|
||||||
{
|
{
|
||||||
Image avatarImage = imageDate.getImage();
|
Image avatarImage = imageDate.getImage();
|
||||||
|
|||||||
@@ -17,8 +17,10 @@
|
|||||||
<!-- /header -->
|
<!-- /header -->
|
||||||
|
|
||||||
<div id="loginContent" data-role="content">
|
<div id="loginContent" data-role="content">
|
||||||
<label for="playerName" class="input">Id:</label>
|
<label for="loginName" class="input">User:</label>
|
||||||
<input type="text" id="playerId" name="playerId" data-mini="true">
|
<input type="text" id="loginName" name="loginName" data-mini="true">
|
||||||
|
<label for="loginPassword" class="input">Password:</label>
|
||||||
|
<input type="text" id="loginPassword" name="loginPassword" data-mini="true">
|
||||||
<br />
|
<br />
|
||||||
<a data-role="button" id="loginButton">Login</a>
|
<a data-role="button" id="loginButton">Login</a>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+81
-18
@@ -2,6 +2,7 @@ var ownProfileId = -1;
|
|||||||
var profileIdRequest = -1;
|
var profileIdRequest = -1;
|
||||||
var songIdRequest = -1
|
var songIdRequest = -1
|
||||||
var allSongsCache = null;
|
var allSongsCache = null;
|
||||||
|
var sessionId = "";
|
||||||
|
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
replaceTransitionHandler();
|
replaceTransitionHandler();
|
||||||
@@ -19,9 +20,14 @@ function replaceTransitionHandler() {
|
|||||||
var promise = $to.data('promise');
|
var promise = $to.data('promise');
|
||||||
if (promise) {
|
if (promise) {
|
||||||
$to.removeData('promise');
|
$to.removeData('promise');
|
||||||
$.mobile.loading('show');
|
$('#content').wrap('<div class="overlay" />');
|
||||||
|
$.mobile.loading('show', {
|
||||||
|
text: 'Loading data...',
|
||||||
|
textVisible: true
|
||||||
|
});
|
||||||
return promise.then(function () {
|
return promise.then(function () {
|
||||||
$.mobile.loading('hide');
|
$.mobile.loading('hide');
|
||||||
|
$('#content').unwrap();
|
||||||
return oldDefaultTransitionHandler(name, reverse, $to, $from);
|
return oldDefaultTransitionHandler(name, reverse, $to, $from);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -33,7 +39,8 @@ function initPageLoadHandler() {
|
|||||||
//pageLoadHandler for displayProfile
|
//pageLoadHandler for displayProfile
|
||||||
$(document).on('pagebeforeshow', '#displayProfile', function () {
|
$(document).on('pagebeforeshow', '#displayProfile', function () {
|
||||||
var promise = $.ajax({
|
var promise = $.ajax({
|
||||||
url: "getProfile?profileId=" + profileIdRequest
|
url: "getProfile?profileId=" + profileIdRequest,
|
||||||
|
headers: { "session": sessionId }
|
||||||
}).done(function (result) {
|
}).done(function (result) {
|
||||||
$('#playerName').prop("value", result.PlayerName);
|
$('#playerName').prop("value", result.PlayerName);
|
||||||
if (result.Avatar && result.Avatar.base64Data) {
|
if (result.Avatar && result.Avatar.base64Data) {
|
||||||
@@ -42,6 +49,12 @@ function initPageLoadHandler() {
|
|||||||
$('#playerType').prop("value", result.Type);
|
$('#playerType').prop("value", result.Type);
|
||||||
$('#playerDifficulty').prop("value", result.Difficulty);
|
$('#playerDifficulty').prop("value", result.Difficulty);
|
||||||
if (result.IsEditable) {
|
if (result.IsEditable) {
|
||||||
|
$('#playerName').prop('disabled', false);
|
||||||
|
$('#playerType').prop('disabled', false);
|
||||||
|
$('#playerDifficulty').prop('disabled', false);
|
||||||
|
$('#playerSaveButton').show();
|
||||||
|
|
||||||
|
$('#playerAvatar').unbind("click");
|
||||||
$('#playerAvatar').click(function () {
|
$('#playerAvatar').click(function () {
|
||||||
if ($('#captureContainer').length > 0) {
|
if ($('#captureContainer').length > 0) {
|
||||||
$('#captureContainer').remove();
|
$('#captureContainer').remove();
|
||||||
@@ -85,6 +98,7 @@ function initPageLoadHandler() {
|
|||||||
dataType: "json",
|
dataType: "json",
|
||||||
contentType: "application/json;charset=utf-8",
|
contentType: "application/json;charset=utf-8",
|
||||||
type: "POST",
|
type: "POST",
|
||||||
|
headers: { "session": sessionId },
|
||||||
data: JSON.stringify(dataToUpload),
|
data: JSON.stringify(dataToUpload),
|
||||||
success: function (msg) {
|
success: function (msg) {
|
||||||
|
|
||||||
@@ -112,7 +126,8 @@ function initPageLoadHandler() {
|
|||||||
//pageLoadHandler for selectProfile
|
//pageLoadHandler for selectProfile
|
||||||
$(document).on('pagebeforeshow', '#selectProfile', function () {
|
$(document).on('pagebeforeshow', '#selectProfile', function () {
|
||||||
var promise = $.ajax({
|
var promise = $.ajax({
|
||||||
url: "getProfileList"
|
url: "getProfileList",
|
||||||
|
headers: { "session": sessionId }
|
||||||
}).done(function (data) {
|
}).done(function (data) {
|
||||||
$('#selectProfileList').children().remove();
|
$('#selectProfileList').children().remove();
|
||||||
|
|
||||||
@@ -137,7 +152,8 @@ function initPageLoadHandler() {
|
|||||||
//pageLoadHandler for displaySong
|
//pageLoadHandler for displaySong
|
||||||
$(document).on('pagebeforeshow', '#displaySong', function () {
|
$(document).on('pagebeforeshow', '#displaySong', function () {
|
||||||
var promise = $.ajax({
|
var promise = $.ajax({
|
||||||
url: "getSong?songId=" + songIdRequest
|
url: "getSong?songId=" + songIdRequest,
|
||||||
|
headers: { "session": sessionId }
|
||||||
}).done(function (result) {
|
}).done(function (result) {
|
||||||
if (result.Title != null) {
|
if (result.Title != null) {
|
||||||
$('#displaySongTitle').text(result.Title);
|
$('#displaySongTitle').text(result.Title);
|
||||||
@@ -209,7 +225,8 @@ function initPageLoadHandler() {
|
|||||||
|
|
||||||
if (allSongsCache == null) {
|
if (allSongsCache == null) {
|
||||||
var promise = $.ajax({
|
var promise = $.ajax({
|
||||||
url: "getAllSongs"
|
url: "getAllSongs",
|
||||||
|
headers: { "session": sessionId }
|
||||||
}).done(function (data) {
|
}).done(function (data) {
|
||||||
allSongsCache = data;
|
allSongsCache = data;
|
||||||
handleGetAllSongs()
|
handleGetAllSongs()
|
||||||
@@ -225,11 +242,47 @@ function initPageLoadHandler() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function initLoginPageHandler() {
|
function initLoginPageHandler() {
|
||||||
$('#loginButton').click(function () {
|
var keyPressed = function (e) {
|
||||||
ownProfileId = parseInt($('#playerId').prop("value"));
|
if (e.which == 13) {
|
||||||
if (ownProfileId != "NaN") {
|
$('#loginButton').click();
|
||||||
$.mobile.changePage("#main", { transition: "slidefade" });
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$('#loginName').keypress(keyPressed)
|
||||||
|
$('#loginPassword').keypress(keyPressed);
|
||||||
|
|
||||||
|
$('#loginButton').click(function () {
|
||||||
|
$('#content').wrap('<div class="overlay" />');
|
||||||
|
$.mobile.loading('show', {
|
||||||
|
text: 'Login...',
|
||||||
|
textVisible: true
|
||||||
|
});
|
||||||
|
|
||||||
|
username = $('#loginName').prop("value");
|
||||||
|
password = $('#loginPassword').prop("value");
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "login?username=" + username + "&password=" + password
|
||||||
|
}).done(function (result) {
|
||||||
|
sessionId = result;
|
||||||
|
$.ajax({
|
||||||
|
url: "getOwnProfileId",
|
||||||
|
headers: { "session": sessionId }
|
||||||
|
}).done(function (result) {
|
||||||
|
ownProfileId = result;
|
||||||
|
|
||||||
|
$.mobile.loading('hide');
|
||||||
|
$('#content').unwrap();
|
||||||
|
|
||||||
|
$.mobile.changePage("#main", { transition: "slidefade" });
|
||||||
|
}).fail(function (result) {
|
||||||
|
$.mobile.loading('hide');
|
||||||
|
$('#content').unwrap();
|
||||||
|
});
|
||||||
|
}).fail(function (result) {
|
||||||
|
$.mobile.loading('hide');
|
||||||
|
$('#content').unwrap();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,7 +300,8 @@ function initMainPageHandler() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "getCurrentSongId"
|
url: "getCurrentSongId",
|
||||||
|
headers: { "session": sessionId }
|
||||||
}).done(function (result) {
|
}).done(function (result) {
|
||||||
songIdRequest = parseInt(result);
|
songIdRequest = parseInt(result);
|
||||||
$.mobile.loading('hide');
|
$.mobile.loading('hide');
|
||||||
@@ -283,6 +337,7 @@ function initMainPageHandler() {
|
|||||||
dataType: "json",
|
dataType: "json",
|
||||||
contentType: "application/json;charset=utf-8",
|
contentType: "application/json;charset=utf-8",
|
||||||
type: "POST",
|
type: "POST",
|
||||||
|
headers: { "session": sessionId },
|
||||||
data: JSON.stringify({ Photo: { base64Data: e.target.result } }),
|
data: JSON.stringify({ Photo: { base64Data: e.target.result } }),
|
||||||
success: function (msg) {
|
success: function (msg) {
|
||||||
|
|
||||||
@@ -304,43 +359,50 @@ function initMainPageHandler() {
|
|||||||
function initKeyboardPageHandler() {
|
function initKeyboardPageHandler() {
|
||||||
$('#keyboardButtonUp').click(function () {
|
$('#keyboardButtonUp').click(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=up"
|
url: "sendKeyEvent?key=up",
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#keyboardButtonDown').click(function () {
|
$('#keyboardButtonDown').click(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=down"
|
url: "sendKeyEvent?key=down",
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#keyboardButtonLeft').click(function () {
|
$('#keyboardButtonLeft').click(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=left"
|
url: "sendKeyEvent?key=left",
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#keyboardButtonRight').click(function () {
|
$('#keyboardButtonRight').click(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=right"
|
url: "sendKeyEvent?key=right",
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#keyboardButtonEscape').click(function () {
|
$('#keyboardButtonEscape').click(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=escape"
|
url: "sendKeyEvent?key=escape",
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#keyboardButtonkeyboardButtonTab').click(function () {
|
$('#keyboardButtonkeyboardButtonTab').click(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=tab"
|
url: "sendKeyEvent?key=tab",
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
$('#keyboardButtonReturn').click(function () {
|
$('#keyboardButtonReturn').click(function () {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=return"
|
url: "sendKeyEvent?key=return",
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -349,7 +411,8 @@ function initKeyboardPageHandler() {
|
|||||||
if (c.match(/\w/)) {
|
if (c.match(/\w/)) {
|
||||||
c = e.keyCode >= 65 ? c.toLowerCase() : c;
|
c = e.keyCode >= 65 ? c.toLowerCase() : c;
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "sendKeyEvent?key=" + c
|
url: "sendKeyEvent?key=" + c,
|
||||||
|
headers: { "session": sessionId }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
var oldText = $('#keyboardButtonKeys')[0].value;
|
var oldText = $('#keyboardButtonKeys')[0].value;
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ namespace VocaluxeLib.Profile
|
|||||||
public string FilePath;
|
public string FilePath;
|
||||||
public string FileName;
|
public string FileName;
|
||||||
public string AvatarFileName;
|
public string AvatarFileName;
|
||||||
|
public byte[] PasswordHash;
|
||||||
|
public byte[] PasswordSalt;
|
||||||
|
public int UserRoles;
|
||||||
|
|
||||||
public EGameDifficulty Difficulty;
|
public EGameDifficulty Difficulty;
|
||||||
|
|
||||||
@@ -104,7 +107,7 @@ namespace VocaluxeLib.Profile
|
|||||||
string filename = string.Empty;
|
string filename = string.Empty;
|
||||||
// ReSharper disable LoopCanBeConvertedToQuery
|
// ReSharper disable LoopCanBeConvertedToQuery
|
||||||
foreach (char chr in PlayerName)
|
foreach (char chr in PlayerName)
|
||||||
// ReSharper restore LoopCanBeConvertedToQuery
|
// ReSharper restore LoopCanBeConvertedToQuery
|
||||||
{
|
{
|
||||||
if (char.IsLetter(chr))
|
if (char.IsLetter(chr))
|
||||||
filename += chr.ToString();
|
filename += chr.ToString();
|
||||||
|
|||||||
Reference in new issue
Block a user