Webserver: Improved error handling and a error message for user information

This commit is contained in:
lukeIam committed 2013-12-13 17:43:25 +01:00
1 parent eb22a8b1a3
commit d4d82e56f1
6 files changed
+112 -28

No files matched your search

+74 -27
View File
@@ -17,7 +17,7 @@ namespace ServerLib
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sendKeyEvent?key={key}")]
bool sendKeyEvent(string key);
void sendKeyEvent(string key);
#region profile
@@ -31,7 +31,7 @@ namespace ServerLib
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sendProfile")]
bool sendProfile(ProfileData profile);
void sendProfile(ProfileData profile);
[OperationContract]
[WebInvoke(Method = "GET",
@@ -54,7 +54,7 @@ namespace ServerLib
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/sendPhoto")]
bool sendPhoto(PhotoData photo);
void sendPhoto(PhotoData photo);
#endregion
@@ -168,21 +168,30 @@ namespace ServerLib
return sessionKey;
}
public bool sendKeyEvent(string key)
public void sendKeyEvent(string key)
{
Guid sessionKey = getSession();
if (sessionKey == Guid.Empty || !SessionControl.requestRight(sessionKey, UserRights.UseKeyboard))
if (sessionKey == Guid.Empty )
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
return false;
}
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
if (CServer.SendKeyEvent == null)
{
return false;
}
return CServer.SendKeyEvent(key);
else if (!SessionControl.requestRight(sessionKey, UserRights.UseKeyboard))
{
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";
}
else
{
CServer.SendKeyEvent(key);
}
}
#region profile
@@ -193,35 +202,51 @@ namespace ServerLib
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
return -1;
}
return SessionControl.getUserIdFromSession(sessionKey);
int profileId = SessionControl.getUserIdFromSession(sessionKey);
if (profileId < 0)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
return -1;
}
return profileId;
}
public bool sendProfile(ProfileData profile)
public void sendProfile(ProfileData profile)
{
Guid sessionKey = getSession();
if (profile.ProfileId != -1) //-1 is the id for a new profile
{
if (sessionKey == Guid.Empty || (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles) &&
SessionControl.getUserIdFromSession(sessionKey) != profile.ProfileId))
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
return false;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
return;
}
if (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles) &&
SessionControl.getUserIdFromSession(sessionKey) != profile.ProfileId)
{
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;
return false;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not found";
return;
}
bool result = CServer.SendProfileData(profile);
CServer.SendProfileData(profile);
if (profile.Password != null && profile.Password != "")
if (!string.IsNullOrEmpty(profile.Password))
{
int profileId = profile.ProfileId;
if (profileId == -1)
@@ -231,18 +256,24 @@ namespace ServerLib
CServer.SetPassword(profileId, profile.Password);
}
return result;
}
public ProfileData getProfile(int profileId)
{
Guid sessionKey = getSession();
if (sessionKey == Guid.Empty || (!SessionControl.requestRight(sessionKey, UserRights.ViewOtherProfiles) &&
SessionControl.getUserIdFromSession(sessionKey) != profileId))
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
return new ProfileData();
}
if (!SessionControl.requestRight(sessionKey, UserRights.ViewOtherProfiles) &&
SessionControl.getUserIdFromSession(sessionKey) != profileId)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
return new ProfileData();
}
@@ -270,16 +301,24 @@ namespace ServerLib
#region photo
public bool sendPhoto(PhotoData photo)
public void sendPhoto(PhotoData photo)
{
Guid sessionKey = getSession();
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
return;
}
if (!SessionControl.requestRight(sessionKey, UserRights.UploadPhotos))
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
return false;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
return;
}
return CServer.SendPhoto(photo);
CServer.SendPhoto(photo);
}
#endregion
@@ -292,6 +331,7 @@ namespace ServerLib
if (sessionId == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Wrong username or password";
}
return sessionId;
}
@@ -424,9 +464,16 @@ namespace ServerLib
public void setUserRole(int profileId, int userRole)
{
Guid sessionKey = getSession();
if (sessionKey == Guid.Empty)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "No session";
return;
}
if (!SessionControl.requestRight(sessionKey, UserRights.EditAllProfiles))
{
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Forbidden;
WebOperationContext.Current.OutgoingResponse.StatusDescription = "Not allowed";
return;
}
CServer.SetUserRole(profileId, userRole);
@@ -59,6 +59,10 @@ namespace ServerLib
internal static int getUserIdFromSession(Guid sessionId)
{
if (!activeSessions.ContainsKey(sessionId))
{
return -1;
}
return activeSessions[sessionId].ProfileId;
}
}
+3
View File
@@ -312,6 +312,9 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CustomToolNamespace>images</CustomToolNamespace>
</Content>
<Content Include="Website\css\images\alert.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Website\css\images\icons-18-black.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CustomToolNamespace>images</CustomToolNamespace>
Binary file not shown.

After

Width:  |  Height:  |  Size: 454 B

+7
View File
@@ -73,3 +73,10 @@
.ui-input-search {
background: #ffffff !important;
}
.ui-custom-errorIcon {
background: url(images/alert.png) !important;
background-size: 42px 42px;
-webkit-border-radius: 0px !important;
border-radius: 0px !important;
}
+24 -1
View File
@@ -598,7 +598,17 @@ function request(data, message) {
$('div[data-role="content"]').unwrap();
}
}).fail(function (result) {
//TODO:Fail Handling
if (result.statusText.indexOf("No session") != -1) {
logout();
return;
}
if (message != "noOverlay") {
var msg = result.statusText.length <= 20 ? result.statusText : "Error...";
if (msg == "error" && result.readyState == 0) {
msg = "No connection";
}
showError(msg);
}
});
}
@@ -606,4 +616,17 @@ function showError(message) {
if (!message) {
message = "Error...";
}
$('div[data-role="content"]').wrap('<div class="overlay" />');
$.mobile.loading('show', {
text: message,
textVisible: true
});
$('.ui-loader').find('span').removeClass('ui-icon-loading').addClass('ui-custom-errorIcon');
setTimeout(function() {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
$('.ui-loader').find('span').removeClass('ui-custom-errorIcon').addClass('ui-icon-loading');
}, 1000);
}