Webserver: renaming

This commit is contained in:
lukeIam committed 2013-12-13 17:43:27 +01:00
1 parent 013fc1e846
commit 3344f05b58
9 files changed
+359 -367

No files matched your search

+81 -84
View File
@@ -1,11 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
using System.Net;
@@ -13,91 +8,91 @@ namespace ServerLib
{
public class CServer
{
private ServiceHost host;
private Uri baseAddress;
private bool encrypted = false;
private ServiceHost _Host;
private Uri _BaseAddress;
private bool _Encrypted = false;
private static SendKeyEventDelegate sendKeyEvent;
private static SendKeyEventDelegate _SendKeyEvent;
public static SendKeyEventDelegate SendKeyEvent
{
internal get { return CServer.sendKeyEvent; }
set { CServer.sendKeyEvent = value; }
internal get { return CServer._SendKeyEvent; }
set { CServer._SendKeyEvent = value; }
}
#region profile
private static GetProfileDataDelegate getProfileData;
private static GetProfileDataDelegate _GetProfileData;
public static GetProfileDataDelegate GetProfileData
{
internal get { return CServer.getProfileData; }
set { CServer.getProfileData = value; }
internal get { return CServer._GetProfileData; }
set { CServer._GetProfileData = value; }
}
private static SendProfileDataDelegate sendProfileData;
private static SendProfileDataDelegate _SendProfileData;
public static SendProfileDataDelegate SendProfileData
{
internal get { return CServer.sendProfileData; }
set { CServer.sendProfileData = value; }
internal get { return CServer._SendProfileData; }
set { CServer._SendProfileData = value; }
}
private static GetProfileListDelegate getProfileList;
private static GetProfileListDelegate _GetProfileList;
public static GetProfileListDelegate GetProfileList
{
internal get { return CServer.getProfileList; }
set { CServer.getProfileList = value; }
internal get { return CServer._GetProfileList; }
set { CServer._GetProfileList = value; }
}
#endregion
#region photo
private static SendPhotoDelegate sendPhoto;
private static SendPhotoDelegate _SendPhoto;
public static SendPhotoDelegate SendPhoto
{
get { return CServer.sendPhoto; }
set { CServer.sendPhoto = value; }
get { return CServer._SendPhoto; }
set { CServer._SendPhoto = value; }
}
#endregion
#region website
private static GetSiteFileDelegate getSiteFile;
private static GetSiteFileDelegate _GetSiteFile;
public static GetSiteFileDelegate GetSiteFile
{
internal get { return CServer.getSiteFile; }
set { CServer.getSiteFile = value; }
internal get { return CServer._GetSiteFile; }
set { CServer._GetSiteFile = value; }
}
private static GetDelayedImageDelegate getDelayedImage;
private static GetDelayedImageDelegate _GetDelayedImage;
public static GetDelayedImageDelegate GetDelayedImage
{
internal get { return CServer.getDelayedImage; }
set { CServer.getDelayedImage = value; }
internal get { return CServer._GetDelayedImage; }
set { CServer._GetDelayedImage = value; }
}
#endregion
#region songs
private static GetSongDelegate getSong;
private static GetSongDelegate _GetSong;
public static GetSongDelegate GetSong
{
internal get { return CServer.getSong; }
set { CServer.getSong = value; }
internal get { return CServer._GetSong; }
set { CServer._GetSong = value; }
}
private static GetAllSongsDelegate getAllSongs;
private static GetAllSongsDelegate _GetAllSongs;
public static GetAllSongsDelegate GetAllSongs
{
get { return CServer.getAllSongs; }
set { CServer.getAllSongs = value; }
get { return CServer._GetAllSongs; }
set { CServer._GetAllSongs = value; }
}
private static GetCurrentSongIdDelegate getCurrentSongId;
private static GetCurrentSongIdDelegate _GetCurrentSongId;
public static GetCurrentSongIdDelegate GetCurrentSongId
{
get { return CServer.getCurrentSongId; }
set { CServer.getCurrentSongId = value; }
get { return CServer._GetCurrentSongId; }
set { CServer._GetCurrentSongId = value; }
}
@@ -105,77 +100,77 @@ namespace ServerLib
#region user management
private static SetPasswordDelegate setPassword;
private static SetPasswordDelegate _SetPassword;
public static SetPasswordDelegate SetPassword
{
internal get { return CServer.setPassword; }
set { CServer.setPassword = value; }
internal get { return CServer._SetPassword; }
set { CServer._SetPassword = value; }
}
private static ValidatePasswordDelegate validatePassword;
private static ValidatePasswordDelegate _ValidatePassword;
public static ValidatePasswordDelegate ValidatePassword
{
internal get { return CServer.validatePassword; }
set { CServer.validatePassword = value; }
internal get { return CServer._ValidatePassword; }
set { CServer._ValidatePassword = value; }
}
private static GetUserRoleDelegate getUserRole;
private static GetUserRoleDelegate _GetUserRole;
public static GetUserRoleDelegate GetUserRole
{
internal get { return CServer.getUserRole; }
set { CServer.getUserRole = value; }
internal get { return CServer._GetUserRole; }
set { CServer._GetUserRole = value; }
}
private static SetUserRoleDelegate setUserRole;
private static SetUserRoleDelegate _SetUserRole;
public static SetUserRoleDelegate SetUserRole
{
internal get { return CServer.setUserRole; }
set { CServer.setUserRole = value; }
internal get { return CServer._SetUserRole; }
set { CServer._SetUserRole = value; }
}
private static GetUserIdFromUsernameDelegate getUserIdFromUsername;
private static GetUserIdFromUsernameDelegate _GetUserIdFromUsername;
public static GetUserIdFromUsernameDelegate GetUserIdFromUsername
{
internal get { return CServer.getUserIdFromUsername; }
set { CServer.getUserIdFromUsername = value; }
internal get { return CServer._GetUserIdFromUsername; }
set { CServer._GetUserIdFromUsername = value; }
}
#endregion
public CServer(int port, bool encrypted)
{
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();
if (encrypted)
{
baseAddress = new Uri("https://" + hostname + ":" + port + "/");
_BaseAddress = new Uri("https://" + hostname + ":" + port + "/");
}
else
{
baseAddress = new Uri("http://" + hostname + ":" + port + "/");
_BaseAddress = new Uri("http://" + hostname + ":" + port + "/");
}
this.encrypted = encrypted;
host = new WebServiceHost(typeof(CWebservice), baseAddress);
this._Encrypted = encrypted;
_Host = new WebServiceHost(typeof(CWebservice), _BaseAddress);
WebHttpBinding wb = new WebHttpBinding();
wb.MaxReceivedMessageSize = 10485760;
wb.MaxBufferSize = 10485760;
wb.MaxBufferPoolSize = 10485760;
wb.ReaderQuotas.MaxStringContentLength = 10485760;
wb.ReaderQuotas.MaxArrayLength = 10485760;
wb.ReaderQuotas.MaxBytesPerRead = 10485760;
WebHttpBinding wb = new WebHttpBinding
{
MaxReceivedMessageSize = 10485760,
MaxBufferSize = 10485760,
MaxBufferPoolSize = 10485760,
ReaderQuotas = {MaxStringContentLength = 10485760, MaxArrayLength = 10485760, MaxBytesPerRead = 10485760}
};
if (encrypted)
{
wb.Security.Mode = WebHttpSecurityMode.Transport;
wb.Security.Transport = new HttpTransportSecurity { ClientCredentialType = HttpClientCredentialType.None };
}
host.AddServiceEndpoint(typeof(ICWebservice), wb, "");
_Host.AddServiceEndpoint(typeof(ICWebservice), wb, "");
}
public void Start()
@@ -187,27 +182,27 @@ namespace ServerLib
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);*/
host.Open();
_Host.Open();
}
catch (CommunicationException e)
{
if (e is AddressAccessDeniedException || e is AddressAlreadyInUseException)
{
registerUrlAndCert(baseAddress.Port);
_RegisterUrlAndCert(_BaseAddress.Port);
try
{
host.Abort();
init(baseAddress.Port, encrypted);
host.Open();
_Host.Abort();
_Init(_BaseAddress.Port, _Encrypted);
_Host.Open();
}
catch (CommunicationException e2)
{
host.Abort();
_Host.Abort();
}
}
else
{
host.Abort();
_Host.Abort();
}
}
}
@@ -216,25 +211,27 @@ namespace ServerLib
{
try
{
host.Close();
_Host.Close();
}
catch (CommunicationException e)
{
host.Abort();
_Host.Abort();
}
}
private void registerUrlAndCert(int port)
private void _RegisterUrlAndCert(int port)
{
#if WIN
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "WebserverInitalConfig.exe";
info.Arguments = port.ToString() + " " + encrypted.ToString();
info.UseShellExecute = true;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Verb = "runas";
ProcessStartInfo info = new ProcessStartInfo
{
FileName = "WebserverInitalConfig.exe",
Arguments = port.ToString() + " " + _Encrypted.ToString(),
UseShellExecute = true,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
Verb = "runas"
};
Process p = Process.Start(info);
if (p != null)
{
+133 -100
View File
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.Web.Services;
using System.IO;
using System.ServiceModel.Channels;
@@ -17,7 +13,7 @@ namespace ServerLib
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sendKeyEvent?key={key}")]
void sendKeyEvent(string key);
void SendKeyEvent(string key);
#region profile
@@ -25,25 +21,25 @@ namespace ServerLib
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/getOwnProfileId")]
int getOwnProfileId();
int GetOwnProfileId();
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sendProfile")]
void sendProfile(ProfileData profile);
void SendProfile(SProfileData profile);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/getProfile?profileId={profileId}")]
ProfileData getProfile(int profileId);
SProfileData GetProfile(int profileId);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/getProfileList")]
ProfileData[] getProfileList();
SProfileData[] GetProfileList();
#endregion
@@ -54,7 +50,7 @@ namespace ServerLib
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sendPhoto")]
void sendPhoto(PhotoData photo);
void SendPhoto(SPhotoData photo);
#endregion
@@ -64,7 +60,7 @@ namespace ServerLib
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/login?username={username}&password={password}")]
Guid login(string username, string password);
Guid Login(string username, string password);
[OperationContract]
[WebInvoke(Method = "GET",
@@ -100,7 +96,7 @@ namespace ServerLib
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/delayedImage?id={id}")]
Base64Image GetDelayedImage(String id);
CBase64Image GetDelayedImage(String id);
#endregion
@@ -110,19 +106,19 @@ namespace ServerLib
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/getSong?songId={songId}")]
SongInfo getSong(int songId);
SOngInfo GetSong(int songId);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/getCurrentSongId")]
int getCurrentSongId();
int GetCurrentSongId();
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/getAllSongs")]
SongInfo[] getAllSongs();
SOngInfo[] GetAllSongs();
#endregion
@@ -132,29 +128,25 @@ namespace ServerLib
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/getUserRole?profileId={profileId}")]
int getUserRole(int profileId);
int GetUserRole(int profileId);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/setUserRole?profileId={profileId}&userRole={userRole}")]
void setUserRole(int profileId, int userRole);
void SetUserRole(int profileId, int userRole);
#endregion
}
class CWebservice : ICWebservice
{
public CWebservice()
{
}
private static Guid getSession()
private static Guid _GetSession()
{
Guid sessionKey = Guid.Empty;
string sessionHeader =
((HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties["httpRequest"]).Headers["session"];
if (sessionHeader == null || sessionHeader == "")
if (string.IsNullOrEmpty(sessionHeader))
{
return sessionKey;
}
@@ -168,25 +160,33 @@ namespace ServerLib
return sessionKey;
}
public void sendKeyEvent(string key)
public void SendKeyEvent(string key)
{
Guid sessionKey = getSession();
Guid sessionKey = _GetSession();
if (sessionKey == Guid.Empty )
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
}
}
else if (!SessionControl.requestRight(sessionKey, UserRights.UseKeyboard))
else if (!CSessionControl.RequestRight(sessionKey, EUserRights.UseKeyboard))
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
}
}
else if (CServer.SendKeyEvent == null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not found";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not found";
}
}
else
{
@@ -196,51 +196,66 @@ namespace ServerLib
#region profile
public int getOwnProfileId()
public int GetOwnProfileId()
{
Guid sessionKey = getSession();
Guid sessionKey = _GetSession();
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
}
return -1;
}
int profileId = SessionControl.getUserIdFromSession(sessionKey);
int profileId = CSessionControl.GetUserIdFromSession(sessionKey);
if (profileId < 0)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
}
return -1;
}
return profileId;
}
public void sendProfile(ProfileData profile)
public void SendProfile(SProfileData profile)
{
Guid sessionKey = getSession();
Guid sessionKey = _GetSession();
if (profile.ProfileId != -1) //-1 is the id for a new profile
{
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
}
return;
}
if (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles) &&
SessionControl.getUserIdFromSession(sessionKey) != profile.ProfileId)
if (!CSessionControl.RequestRight(sessionKey, EUserRights.EditAllProfiles) &&
CSessionControl.GetUserIdFromSession(sessionKey) != profile.ProfileId)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
}
return;
}
}
if (CServer.SendProfileData == null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not found";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not found";
}
return;
}
@@ -258,41 +273,47 @@ namespace ServerLib
}
}
public ProfileData getProfile(int profileId)
public SProfileData GetProfile(int profileId)
{
Guid sessionKey = getSession();
Guid sessionKey = _GetSession();
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
return new ProfileData();
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
}
return new SProfileData();
}
if (!SessionControl.requestRight(sessionKey, UserRights.ViewOtherProfiles) &&
SessionControl.getUserIdFromSession(sessionKey) != profileId)
if (!CSessionControl.RequestRight(sessionKey, EUserRights.ViewOtherProfiles) &&
CSessionControl.GetUserIdFromSession(sessionKey) != profileId)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
return new ProfileData();
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
}
return new SProfileData();
}
if (CServer.GetProfileData == null)
{
return new ProfileData();
return new SProfileData();
}
bool isReadonly = (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles) &&
SessionControl.getUserIdFromSession(sessionKey) != profileId);
bool isReadonly = (!CSessionControl.RequestRight(sessionKey, EUserRights.EditAllProfiles) &&
CSessionControl.GetUserIdFromSession(sessionKey) != profileId);
return CServer.GetProfileData(profileId, isReadonly);
}
public ProfileData[] getProfileList()
public SProfileData[] GetProfileList()
{
if (CServer.GetProfileList == null)
{
return new ProfileData[] { };
return new SProfileData[] { };
}
return CServer.GetProfileList();
}
@@ -301,20 +322,26 @@ namespace ServerLib
#region photo
public void sendPhoto(PhotoData photo)
public void SendPhoto(SPhotoData photo)
{
Guid sessionKey = getSession();
Guid sessionKey = _GetSession();
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
}
return;
}
if (!SessionControl.requestRight(sessionKey, UserRights.UploadPhotos))
if (!CSessionControl.RequestRight(sessionKey, EUserRights.UploadPhotos))
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
}
return;
}
@@ -325,13 +352,16 @@ namespace ServerLib
#region website
public Guid login(string username, string password)
public Guid Login(string username, string password)
{
Guid sessionId = SessionControl.openSession(username, password);
Guid sessionId = CSessionControl.OpenSession(username, password);
if (sessionId == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Wrong username or password";
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Wrong username or password";
}
}
return sessionId;
}
@@ -359,11 +389,12 @@ namespace ServerLib
{
return new MemoryStream(data);
}
else
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
return null;
}
return null;
}
public Stream GetCssFile(string filename)
@@ -379,13 +410,11 @@ namespace ServerLib
{
return new MemoryStream(data);
}
else
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
return null;
}
return null;
}
public Stream GetCssImageFile(string filename)
@@ -401,11 +430,11 @@ namespace ServerLib
{
return new MemoryStream(data);
}
else
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
return null;
}
return null;
}
public Stream GetImgFile(string filename)
@@ -421,14 +450,14 @@ namespace ServerLib
{
return new MemoryStream(data);
}
else
if (WebOperationContext.Current != null)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
return null;
}
return null;
}
public Base64Image GetDelayedImage(string id)
public CBase64Image GetDelayedImage(string id)
{
return CServer.GetDelayedImage(id);
}
@@ -437,43 +466,47 @@ namespace ServerLib
#region songs
public SongInfo getSong(int songId)
public SOngInfo GetSong(int songId)
{
return CServer.GetSong(songId);
}
public int getCurrentSongId()
public int GetCurrentSongId()
{
return CServer.GetCurrentSongId();
}
public SongInfo[] getAllSongs()
public SOngInfo[] GetAllSongs()
{
return CServer.GetAllSongs();
}
#endregion
#region user management
#region user management
public int getUserRole(int profileId)
public int GetUserRole(int profileId)
{
return CServer.GetUserRole(profileId);
}
public void setUserRole(int profileId, int userRole)
public void SetUserRole(int profileId, int userRole)
{
Guid sessionKey = getSession();
Guid sessionKey = _GetSession();
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
if (WebOperationContext.Current != null) {
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
}
return;
}
if (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles))
if (!CSessionControl.RequestRight(sessionKey, EUserRights.EditAllProfiles))
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
if (WebOperationContext.Current != null) {
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
}
return;
}
CServer.SetUserRole(profileId, userRole);
+18 -20
View File
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Drawing;
using System.IO;
@@ -15,15 +13,15 @@ namespace ServerLib
#region profile
public delegate ProfileData GetProfileDataDelegate(int profileId, bool isReadonly);
public delegate bool SendProfileDataDelegate(ProfileData profile);
public delegate ProfileData[] GetProfileListDelegate();
public delegate SProfileData GetProfileDataDelegate(int profileId, bool isReadonly);
public delegate bool SendProfileDataDelegate(SProfileData profile);
public delegate SProfileData[] GetProfileListDelegate();
[DataContract]
public struct ProfileData
public struct SProfileData
{
[DataMember]
public Base64Image Avatar;
public CBase64Image Avatar;
[DataMember]
public string PlayerName;
[DataMember]
@@ -42,13 +40,13 @@ namespace ServerLib
#region photo
public delegate bool SendPhotoDelegate(PhotoData photo);
public delegate bool SendPhotoDelegate(SPhotoData photo);
[DataContract]
public struct PhotoData
public struct SPhotoData
{
[DataMember]
public Base64Image Photo;
public CBase64Image Photo;
//Add infomation about the user who took this image??
}
@@ -58,12 +56,12 @@ namespace ServerLib
public delegate byte[] GetSiteFileDelegate(string filename);
public delegate Base64Image GetDelayedImageDelegate(string hashedFilename);
public delegate CBase64Image GetDelayedImageDelegate(string hashedFilename);
#endregion
[DataContract]
public class Base64Image
public class CBase64Image
{
[DataMember]
private string base64Data = "";
@@ -71,7 +69,7 @@ namespace ServerLib
[DataMember]
private string imageId = "";
public Base64Image(Image img, ImageFormat format)
public CBase64Image(Image img, ImageFormat format)
{
MemoryStream ms = new MemoryStream();
img.Save(ms, format);
@@ -79,12 +77,12 @@ namespace ServerLib
base64Data = "data:image/" + formatString + ";base64," + Convert.ToBase64String(ms.ToArray());
}
public Base64Image(string imageId)
public CBase64Image(string imageId)
{
this.imageId = imageId;
}
public Image getImage()
public Image GetImage()
{
string onlyBase64Data = base64Data.Substring(base64Data.IndexOf(";base64,") + (";base64,").Length);
byte[] imageData = Convert.FromBase64String(onlyBase64Data);
@@ -94,7 +92,7 @@ namespace ServerLib
return image;
}
public string getImageType()
public string GetImageType()
{
Match match = Regex.Match(base64Data, "(?<=data:image/)[a-zA-Z]+(?=;base64)");
return match.Success ? match.Groups[0].Value : "";
@@ -105,21 +103,21 @@ namespace ServerLib
#region songs
public delegate SongInfo GetSongDelegate(int songId);
public delegate SOngInfo GetSongDelegate(int songId);
public delegate SongInfo[] GetAllSongsDelegate();
public delegate SOngInfo[] GetAllSongsDelegate();
public delegate int GetCurrentSongIdDelegate();
[DataContract]
public struct SongInfo
public struct SOngInfo
{
[DataMember]
public string Title;
[DataMember]
public string Artist;
[DataMember]
public Base64Image Cover;
public CBase64Image Cover;
[DataMember]
public string Genre { get; set; }
[DataMember]
+12 -15
View File
@@ -1,35 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServerLib
{
internal class Session
internal class CSession
{
Guid id;
readonly Guid _ID;
public Guid Id
{
get { return id; }
get { return _ID; }
}
int profileId;
readonly int _ProfileId;
public int ProfileId
{
get { return profileId; }
get { return _ProfileId; }
}
UserRoles roles;
internal UserRoles Roles
readonly EUserRoles _Roles;
internal EUserRoles Roles
{
get { return roles; }
get { return _Roles; }
}
public Session(Guid id, int profileId, UserRoles roles)
public CSession(Guid id, int profileId, EUserRoles roles)
{
this.id = id;
this.profileId = profileId;
this.roles = roles;
this._ID = id;
this._ProfileId = profileId;
this._Roles = roles;
}
}
}
@@ -1,69 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServerLib
{
internal static class SessionControl
internal static class CSessionControl
{
private static Dictionary<Guid, Session> activeSessions = new Dictionary<Guid, Session>();
private static readonly Dictionary<Guid, CSession> _ActiveSessions = new Dictionary<Guid, CSession>();
public static Guid openSession(string userName, string password)
public static Guid OpenSession(string userName, string password)
{
if (!validateUserAndPassword(userName, 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);
int id = _GetProfileIdFormUsername(userName);
EUserRoles roles = _GetUserRoles(id);
CSession session = new CSession(newId, id, roles);
InvalidateSessions(id);
_ActiveSessions.Add(newId, session);
return newId;
}
private static bool validateUserAndPassword(string userName, string password)
private static bool _ValidateUserAndPassword(string userName, string password)
{
return CServer.ValidatePassword(getProfileIdFormUsername(userName), password);
return CServer.ValidatePassword(_GetProfileIdFormUsername(userName), password);
}
private static int getProfileIdFormUsername(string username)
private static int _GetProfileIdFormUsername(string username)
{
return CServer.GetUserIdFromUsername(username);
}
private static UserRoles getUserRoles(int profileId)
private static EUserRoles _GetUserRoles(int profileId)
{
return (UserRoles)CServer.GetUserRole(profileId);
return (EUserRoles)CServer.GetUserRole(profileId);
}
internal static void invalidateSessions(int profileId)
internal static void InvalidateSessions(int profileId)
{
foreach (var s in (from kv in activeSessions
foreach (var s in (from kv in _ActiveSessions
where kv.Value.ProfileId == profileId
select kv).ToList())
{
activeSessions.Remove(s.Key);
_ActiveSessions.Remove(s.Key);
}
}
internal static bool requestRight(Guid sessionId, UserRights requestedRight)
internal static bool RequestRight(Guid sessionId, EUserRights requestedRight)
{
return ((UserRoleControl.getUserRightsFromUserRole(activeSessions[sessionId].Roles) & requestedRight) != 0);
return ((CUserRoleControl.GetUserRightsFromUserRole(_ActiveSessions[sessionId].Roles) & requestedRight) != 0);
}
internal static int getUserIdFromSession(Guid sessionId)
internal static int GetUserIdFromSession(Guid sessionId)
{
if (!activeSessions.ContainsKey(sessionId))
if (!_ActiveSessions.ContainsKey(sessionId))
{
return -1;
}
return activeSessions[sessionId].ProfileId;
return _ActiveSessions[sessionId].ProfileId;
}
}
}
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServerLib
{
[Flags]
internal enum UserRights
internal enum EUserRights
{
None = 0x00,
EditAllProfiles = 0x01,
@@ -1,35 +1,28 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServerLib
{
internal static class UserRoleControl
internal static class CUserRoleControl
{
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)}
private static readonly Dictionary<EUserRoles, EUserRights> _RoleRightsMapping = new Dictionary<EUserRoles, EUserRights> {
{EUserRoles.Administrator,
(EUserRights.EditAllProfiles|
EUserRights.UploadPhotos|
EUserRights.ViewOtherProfiles)|
EUserRights.UseKeyboard},
{EUserRoles.AuthenticatedUser,
(EUserRights.UploadPhotos|
EUserRights.ViewOtherProfiles|
EUserRights.UseKeyboard)}
};
internal static UserRights getUserRightsFromUserRole(UserRoles userRole)
internal static EUserRights GetUserRightsFromUserRole(EUserRoles userRole)
{
UserRights resultRights = UserRights.None;
foreach (UserRights r in roleRightsMapping.Where(role => userRole.HasFlag(role.Key)).Select(role => role.Value))
{
resultRights |= r;
}
return resultRights;
return _RoleRightsMapping.Where(role => userRole.HasFlag(role.Key))
.Select(role => role.Value)
.Aggregate(EUserRights.None, (current, r) => current | r);
}
}
}
+1 -4
View File
@@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ServerLib
{
[Flags]
internal enum UserRoles
internal enum EUserRoles
{
AuthenticatedUser = 0x00,
Administrator = 0x01