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

After

Width:  |  Height:  |  Size: 454 B

+7
View File
@@ -73,3 +73,10 @@
.ui-input-search { .ui-input-search {
background: #ffffff !important; 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(); $('div[data-role="content"]').unwrap();
} }
}).fail(function (result) { }).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) { if (!message) {
message = "Error..."; 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);
} }