public static bool IsLoggedIn (WebServiceResponse response) { if (response == null) return false; return !string.IsNullOrEmpty (response.UserName); }
public static bool IsInRole (WebServiceResponse response, string role) { if (response.UserRoles == null) return false; return Array.IndexOf (response.UserRoles, role) >= 0; }
public static bool IsInRole (WebServiceResponse response, string role) { bool result; if (response == null) { log.Debug ("IsInRole: no response"); return false; } if (response.UserRoles == null) { log.Debug ("IsInRole: no userroles"); return false; } result = Array.IndexOf (response.UserRoles, role) >= 0; log.DebugFormat ("IsInRole ({0}) => {1} (roles: {2})", role, result, string.Join (";", response.UserRoles)); return result; }
public static bool IsInRole (WebServiceResponse response, string role) { bool result; if (response == null) { MonkeyWrench.Logger.Log (2, "IsInRole: no response"); return false; } if (response.UserRoles == null) { MonkeyWrench.Logger.Log (2, "IsInRole: no userroles"); return false; } result = Array.IndexOf (response.UserRoles, role) >= 0; MonkeyWrench.Logger.Log (2, "IsInRole ({0}) => {1} (roles: {2})", role, result, string.Join (";", response.UserRoles)); return result; }
/// <summary> /// Authenticates the request with the provided user/pass. /// If no user/pass is provided, the method returns a response /// with no roles. /// If a wrong user/pass is provided, the method throws an exception. /// </summary> /// <param name="db"></param> /// <param name="login"></param> /// <param name="response"></param> public static void Authenticate (HttpContext Context, DB db, WebServiceLogin login, WebServiceResponse response, bool @readonly) { Authenticate (Context.Request.UserHostAddress, db, login, response, @readonly); }
internal void Authenticate (DB db, WebServiceLogin login, WebServiceResponse response, bool @readonly) { Authentication.Authenticate (Context, db, login, response, @readonly); }
public WebServiceResponse MarkAsDontBuild (WebServiceLogin login, int lane_id) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "UPDATE RevisionWork SET state = 11 WHERE state = 0 AND lane_id = @lane_id;"; DB.CreateParameter (cmd, "lane_id", lane_id); cmd.ExecuteNonQuery (); } } return response; }
public WebServiceResponse AddRelease (WebServiceLogin login, DBRelease release) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.BuildBot); release.Save (db); } return response; }
public WebServiceResponse EditNotification (WebServiceLogin login, DBNotification notification) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); notification.Save (db); Notifications.Restart (); } return response; }
public WebServiceResponse EditIdentity (WebServiceLogin login, DBIrcIdentity irc_identity, DBEmailIdentity email_identity) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); if (irc_identity != null) { irc_identity.Save (db); } if (email_identity != null) { email_identity.Save (db); } } return response; }
public WebServiceResponse DeleteAllWorkForHost (WebServiceLogin login, int host_id) { WebServiceResponse response = new WebServiceResponse (); try { using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = @" DELETE FROM Work WHERE revisionwork_id IN (SELECT id FROM RevisionWork WHERE host_id = @host_id); UPDATE RevisionWork SET state = 10, workhost_id = DEFAULT, completed = DEFAULT WHERE host_id = @host_id; "; DB.CreateParameter (cmd, "host_id", host_id); cmd.ExecuteNonQuery (); } } } catch (Exception ex) { response.Exception = new WebServiceException (ex); } return response; }
public WebServiceResponse ClearAllWorkForHost (WebServiceLogin login, int host_id) { WebServiceResponse response = new WebServiceResponse (); try { using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = @" UPDATE Work SET state = DEFAULT, summary = DEFAULT, starttime = DEFAULT, endtime = DEFAULT, duration = DEFAULT, logfile = DEFAULT, host_id = DEFAULT WHERE Work.revisionwork_id IN (SELECT RevisionWork.id FROM RevisionWork WHERE RevisionWork.host_id = @host_id); UPDATE RevisionWork SET state = DEFAULT, lock_expires = DEFAULT, completed = DEFAULT, workhost_id = DEFAULT WHERE host_id = @host_id; "; DB.CreateParameter (cmd, "host_id", host_id); cmd.ExecuteNonQuery (); } } } catch (Exception ex) { response.Exception = new WebServiceException (ex); } return response; }
public static void VerifyUserInRole (HttpContext Context, DB db, WebServiceLogin login, string role, bool @readonly) { WebServiceResponse dummy = new WebServiceResponse (); Authenticate (Context, db, login, dummy, @readonly); if (!dummy.IsInRole (role)) { log.InfoFormat ("The user '{0}' has the roles '{1}', and requested role is: {2}", login.User, dummy.UserRoles == null ? "<null>" : string.Join (",", dummy.UserRoles), role); throw new UnauthorizedException ("You don't have the required permissions."); } }
/// <summary> /// Authenticates the request with the provided user/pass. /// If no user/pass is provided, the method returns a response /// with no roles. /// If a wrong user/pass is provided, the method throws an exception. /// </summary> /// <param name="db"></param> /// <param name="login"></param> /// <param name="response"></param> public static void Authenticate (HttpContext Context, DB db, WebServiceLogin login, WebServiceResponse response, bool @readonly) { string ip = Context.Request.UserHostAddress; int person_id; DBLoginView view = null; Console.WriteLine ("WebService.Authenticate (Ip4: {0}, UserHostAddress: {1}, User: {2}, Cookie: {3}, Password: {4}", login == null ? null : login.Ip4, Context.Request.UserHostAddress, login == null ? null : login.User, login == null ? null : login.Cookie, login == null ? null : login.Password); // Check if credentials were passed in if (login == null || string.IsNullOrEmpty (login.User) || (string.IsNullOrEmpty (login.Password) && string.IsNullOrEmpty (login.Cookie))) { Console.WriteLine ("No credentials."); return; } if (@readonly && string.IsNullOrEmpty (login.Password)) { // Console.WriteLine ("Readonly authentication needs a password."); return; } if (!string.IsNullOrEmpty (login.Ip4)) { ip = login.Ip4; } else { ip = Context.Request.UserHostAddress; } if (@readonly) { DBLogin result = DBLogin_Extensions.Login (db, login.User, login.Password, ip, @readonly); if (result == null) { // Console.WriteLine ("Incorrect Login/Password for readonly login"); return; } person_id = result.person_id; } else { if (!string.IsNullOrEmpty (login.Password)) { DBLogin result = DBLogin_Extensions.Login (db, login.User, login.Password, ip, @readonly); if (result != null) view = DBLoginView_Extensions.VerifyLogin (db, login.User, result.cookie, ip); } else { view = DBLoginView_Extensions.VerifyLogin (db, login.User, login.Cookie, ip); Console.WriteLine ("Verifying login, cookie: {0} user: {1} ip: {2}", login.Cookie, login.User, ip); } if (view == null) { Console.WriteLine ("Invalid credentials."); return; } person_id = view.person_id; } Console.WriteLine ("Valid credentials"); LoginResponse login_response = response as LoginResponse; if (login_response != null) { login_response.Cookie = view != null ? view.cookie : null; login_response.FullName = view != null ? view.fullname : null; login_response.ID = person_id; } DBPerson person = DBPerson_Extensions.Create (db, person_id); Console.WriteLine ("Roles for '{0}': {1}", login.User, person.roles); if (!string.IsNullOrEmpty (person.roles)) response.UserRoles = person.roles.Split (new char [] { ',' }, StringSplitOptions.RemoveEmptyEntries); }
private void SetResponse (WebServiceResponse response) { this.response = response; LoadView (); }
public WebServiceResponse RemoveUserEmail (WebServiceLogin login, int? id, string username, string email) { WebServiceResponse response = new WebServiceResponse (); DBPerson user; using (DB db = new DB ()) { Authenticate (db, login, response, true); user = FindUser (db, id, username); if (user == null) { /* user doesn't exist */ response.Exception = new WebServiceException (new HttpException (403, "You're not allowed to edit this user")); } else if (Utilities.IsInRole (response, Roles.Administrator)) { /* admin editing (or adming editing self) */ user.RemoveEmail (db, email); } else if (response.UserName == user.login) { /* editing self */ user.RemoveEmail (db, email); } else { /* somebody else editing some other person */ response.Exception = new WebServiceException (new HttpException (403, "You're not allowed to edit this user")); } } return response; }
public WebServiceResponse EditUser (WebServiceLogin login, DBPerson user) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { Authenticate (db, login, response, true); if (user.id == 0) { /* new user, anybody can create new users */ /* create a new person object, and only copy over the fields self is allowed to edit */ if (string.IsNullOrEmpty (user.password) || user.password.Length < 8) { response.Exception = new WebServiceException ("Password must be at least 8 characters long"); return response; } DBPerson person = new DBPerson (); person.fullname = user.fullname; person.login = user.login; person.password = user.password; person.irc_nicknames = user.irc_nicknames; person.Save (db); } else { if (Utilities.IsInRole (response, Roles.Administrator)) { /* admin editing (or adming editing self) */ user.Save (db); // no restrictions } else if (response.UserName == user.login) { /* editing self */ /* create another person object, and only copy over the fields self is allowed to edit */ DBPerson person = DBPerson_Extensions.Create (db, user.id); person.fullname = user.fullname; person.password = user.password; person.irc_nicknames = user.irc_nicknames; person.Save (db); } else { /* somebody else editing some other person */ response.Exception = new WebServiceException (new HttpException (403, "You're not allowed to edit this user")); } } } return response; }
public WebServiceResponse DeleteAllRevisionsForLane (WebServiceLogin login, int lane_id) { WebServiceResponse response = new WebServiceResponse (); try { using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = @"DELETE FROM Revision WHERE lane_id = @lane_id;"; DB.CreateParameter (cmd, "lane_id", lane_id); cmd.ExecuteNonQuery (); } } } catch (Exception ex) { response.Exception = new WebServiceException (ex); } return response; }
public WebServiceResponse RemoveIdentity (WebServiceLogin login, int? irc_identity, int? email_identity) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = string.Empty; if (irc_identity.HasValue) { cmd.CommandText += "DELETE FROM IrcIdentity WHERE id = @irc_id;"; DB.CreateParameter (cmd, "irc_id", irc_identity.Value); } if (email_identity.HasValue) { cmd.CommandText += "DELETE FROM EmailIdentity WHERE id = @email_id;"; DB.CreateParameter (cmd, "email_id", email_identity.Value); } cmd.ExecuteNonQuery (); } } return response; }
public WebServiceResponse RemoveLaneNotification (WebServiceLogin login, int id) { WebServiceResponse response = new WebServiceResponse (); try { using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "DELETE FROM LaneNotification WHERE id = @id;"; DB.CreateParameter (cmd, "id", id); cmd.ExecuteNonQuery (); Notifications.Restart (); } } } catch (Exception ex) { response.Exception = new WebServiceException (ex); } return response; }
public WebServiceResponse AddLaneNotification (WebServiceLogin login, int lane_id, int notification_id) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "INSERT INTO LaneNotification (lane_id, notification_id) VALUES (@lane_id, @notification_id);"; DB.CreateParameter (cmd, "lane_id", lane_id); DB.CreateParameter (cmd, "notification_id", notification_id); cmd.ExecuteNonQuery (); Notifications.Restart (); } } return response; }
public WebServiceResponse AddRelease (WebServiceLogin login, DBRelease release) { WebServiceResponse response = new WebServiceResponse (); try { using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.BuildBot); release.Save (db); } } catch (Exception ex) { response.Exception = new WebServiceException (ex); } return response; }
public WebServiceResponse DeleteRelease (WebServiceLogin login, int id) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "DELETE FROM Release WHERE id = @id;"; DB.CreateParameter (cmd, "id", id); cmd.ExecuteNonQuery (); } } return response; }
public WebServiceResponse ClearAllWorkForLane (WebServiceLogin login, int lane_id) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = @" UPDATE Work SET state = DEFAULT, summary = DEFAULT, starttime = DEFAULT, endtime = DEFAULT, duration = DEFAULT, logfile = DEFAULT, host_id = DEFAULT WHERE Work.revisionwork_id IN (SELECT RevisionWork.id FROM RevisionWork WHERE RevisionWork.lane_id = @lane_id); UPDATE RevisionWork SET state = DEFAULT, lock_expires = DEFAULT, completed = DEFAULT, workhost_id = DEFAULT WHERE lane_id = @lane_id; "; DB.CreateParameter (cmd, "lane_id", lane_id); cmd.ExecuteNonQuery (); Audit (login, "cleared all work for lane {0}", lane_id); } } return response; }
internal void Authenticate (DB db, WebServiceLogin login, WebServiceResponse response) { Authenticate (db, login, response, false); }
public WebServiceResponse DeleteAllWorkForLane (WebServiceLogin login, int lane_id) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = @" DELETE FROM Work WHERE revisionwork_id IN (SELECT id FROM RevisionWork WHERE lane_id = @lane_id); UPDATE RevisionWork SET state = 10, workhost_id = DEFAULT, completed = DEFAULT WHERE lane_id = @lane_id; "; DB.CreateParameter (cmd, "lane_id", lane_id); cmd.ExecuteNonQuery (); } Audit (login, "deleted all work for lane {0}", lane_id); } return response; }
public static void VerifyUserInRole (string remote_ip, DB db, WebServiceLogin login, string role, bool @readonly) { WebServiceResponse dummy = new WebServiceResponse (); Authenticate (remote_ip, db, login, dummy, @readonly); if (!dummy.IsInRole (role)) { Logger.Log (2, "The user '{0}' has the roles '{1}', and requested role is: {2}", login.User, dummy.UserRoles == null ? "<null>" : string.Join (",", dummy.UserRoles), role); throw new HttpException (403, "You don't have the required permissions."); } }
public WebServiceResponse DeleteAllRevisionsForLane (WebServiceLogin login, int lane_id) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = @"DELETE FROM Revision WHERE lane_id = @lane_id;"; DB.CreateParameter (cmd, "lane_id", lane_id); cmd.ExecuteNonQuery (); } } Audit (login, "deleted all revisions for lane {0}", lane_id); return response; }
public static void Authenticate (string user_host_address, DB db, WebServiceLogin login, WebServiceResponse response, bool @readonly) { string ip = user_host_address; int person_id; DBLoginView view = null; Logger.Log (2, "WebService.Authenticate (Ip4: {0}, UserHostAddress: {1}, User: {2}, Cookie: {3}, Password: {4}", login == null ? null : login.Ip4, user_host_address, login == null ? null : login.User, login == null ? null : login.Cookie, login == null ? null : login.Password); // Check if credentials were passed in if (login == null || string.IsNullOrEmpty (login.User) || (string.IsNullOrEmpty (login.Password) && string.IsNullOrEmpty (login.Cookie))) { Logger.Log (2, "No credentials."); VerifyAnonymousAllowed (); return; } if (!string.IsNullOrEmpty (login.Ip4)) { ip = login.Ip4; } else { ip = user_host_address; } if (!string.IsNullOrEmpty (login.Password)) { DBLogin result = DBLogin_Extensions.Login (db, login.User, login.Password, ip, @readonly); if (result != null) { if (@readonly) { person_id = result.person_id; } else { view = DBLoginView_Extensions.VerifyLogin (db, login.User, result.cookie, ip); if (view == null) { Logger.Log (2, "Invalid cookie"); VerifyAnonymousAllowed(); return; } person_id = view.person_id; } } else { Logger.Log (2, "Invalid user/password"); VerifyAnonymousAllowed (); return; } } else { view = DBLoginView_Extensions.VerifyLogin (db, login.User, login.Cookie, ip); if (view == null) { Logger.Log (2, "Invalid cookie"); VerifyAnonymousAllowed (); return; } person_id = view.person_id; Logger.Log (2, "Verifying login, cookie: {0} user: {1} ip: {2}", login.Cookie, login.User, ip); } Logger.Log (2, "Valid credentials"); if (response == null) return; DBPerson person = DBPerson_Extensions.Create (db, person_id); LoginResponse login_response = response as LoginResponse; if (login_response != null) { login_response.Cookie = view != null ? view.cookie : null; login_response.FullName = person.fullname; login_response.ID = person_id; } response.UserName = person.login; response.UserRoles = person.Roles; Logger.Log (2, "Authenticate2 Roles are: {0}", response.UserRoles == null ? "null" : string.Join (";", response.UserRoles)); }
public WebServiceResponse DeleteUser (WebServiceLogin login, int id) { WebServiceResponse response = new WebServiceResponse (); using (DB db = new DB ()) { VerifyUserInRole (db, login, Roles.Administrator); using (IDbCommand cmd = db.CreateCommand ()) { cmd.CommandText = "DELETE FROM Person WHERE id = @id RETURNING login;"; DB.CreateParameter (cmd, "id", id); string user = (string)cmd.ExecuteScalar (); Audit (login, "deleted user {0} (was {1})", id, user); } } return response; }