public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model) { string lLoginCookie = new Tuple<object, object>(model.AuthenticatedClient.GetHashCode(), DateTime.Now.GetHashCode()).GetHashCode().ToString(); using (umfrageDB db = new umfrageDB()) { string lAuthHash = MD5.Create() .ComputeHash(Encoding.UTF8.GetBytes(model.AuthenticatedClient.UserInformation.Id)) .ToHex(false); if ((from p in db.users where p.AuthenticationId == lAuthHash select p).Any()) { db.users.Where(user => user.AuthenticationId == lAuthHash) .Set(user => user.Guid, lLoginCookie).Set(user => user.LastLogin, DateTime.Now) .Update(); } else { user lUser = new user { AuthenticationId = lAuthHash, Guid = lLoginCookie, TimeCreated = DateTime.Now, LastLogin = DateTime.Now }; db.Insert(lUser); } } return nancyModule.Response.AsRedirect("/").WithCookie("id", lLoginCookie); }
public SubmitModule() { this.Get["/submit/{frageId:int}"] = o => { user lUser = this.GetUserFromId(this.Request.Cookies["id"]); DynamicDictionary lQueryDictionary = this.Request.Query as DynamicDictionary; if (lQueryDictionary == null || lUser == null || !this.CheckIfCanAnswer(o.frageId, lUser) || (!lQueryDictionary.ContainsKey("answer") && !lQueryDictionary.ContainsKey("alt_answer"))) return this.Response.AsRedirect("/").WithCookie("error", "true"); using (umfrageDB db = new umfrageDB()) { answer lLog = new answer { FrageId = o.frageId, AnswerId = lQueryDictionary.ContainsKey("alt_answer") ? -1 : lQueryDictionary["answer"], UserId = lUser.AuthenticationId }; if (lQueryDictionary.ContainsKey("alt_answer")) lLog.AltAntwort = lQueryDictionary["alt_answer"]; db.Insert(lLog); } return this.Response.AsRedirect("/"); }; }
public LoginModule() { this.Post["/login"] = _ => { using (umfrageDB db = new umfrageDB()) { string lPassword = this.Request.Form["password"]; string lEncoded = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(lPassword.ToCharArray())).ToHex(false); string lLoginCookie = new Tuple<object, object>(this.Request.GetHashCode(), DateTime.Now.GetHashCode()).GetHashCode() .ToString(); if ((from p in db.users where p.AuthenticationId == lEncoded select p).Any()) { db.users.Where(user => user.AuthenticationId == lEncoded) .Set(user => user.Guid, lLoginCookie).Set(user => user.LastLogin, DateTime.Now) .Update(); return this.Response.AsRedirect("/").WithCookie("id", lLoginCookie); } return this.Response.AsRedirect("/") .WithCookie("error", "true") .WithCookie("error_text", "Das Passwort wurde nicht erkannt!"); } }; this.Get["/logout"] = _ => this.Response.AsRedirect("/").WithCookie("id", ""); }
public FrageModel(question frage) { this.Id = frage.Id; this.FrageText = frage.FrageText; this.Category = frage.Type.Equals("student") ? "Schüler" : "Lehrer"; this.Informal = this.Category.Equals("Schüler"); using (umfrageDB db = new umfrageDB()) { switch (frage.Sex) { case 'm': this.NameZusatz = frage.Type.Equals("students") ? ", Jungen" : ", Männer"; break; case 'f': this.NameZusatz = frage.Type.Equals("students") ? ", Mädchen" : ", Frauen"; break; } this.Options = (from option in db.options where option.type.Equals(frage.Type) && option.sex.Equals(frage.Sex) select new OptionModel(option)).ToArray().OrderBy(model => { string[] lName = model.Name.Split(' '); return this.Informal ? model.Name : lName[lName.Length - 1]; }); } }
public IndexModule() { this.Get["/"] = _ => { bool lIsError = this.Request.Cookies.Keys.Any(s => s.Equals("error") && this.Request.Cookies[s].Equals("true")); string lErrorText = this.Request.Cookies.Keys.Any(s => s.Equals("error_text")) ? this.Request.Cookies["error_text"] : string.Empty; if (!this.Request.Cookies.ContainsKey("id")) return this.View["index"].WithModel(new ResponseModel<object>(new object(), lIsError, lErrorText)).WithCookie(new NancyCookie("error", "false")); using (umfrageDB db = new umfrageDB()) { if (!(from p in db.users where p.Guid == this.Request.Cookies["id"] select p).Any()) return this.View["index"]; question lFrage = QuestionDistributionUtility.FirstUnansweredQuestion(this.Request.Cookies["id"]); return lFrage != null ? lFrage.Pair ? this.View["pair"].WithModel( new ResponseModel<PairModel>(new PairModel(lFrage), lIsError, lErrorText)).WithCookie(new NancyCookie("error", "false")) : this.View["frage"].WithModel( new ResponseModel<FrageModel>(new FrageModel(lFrage), lIsError, lErrorText)).WithCookie(new NancyCookie("error", "false")) : this.View["finished"]; } }; }
private user GetUserFromId(string id) { using (umfrageDB db = new umfrageDB()) { IQueryable<user> lUserQuery = from user in db.users where user.Guid == id select user; if (lUserQuery.Any()) return lUserQuery.First(); } return null; }
private bool CheckIfCanAnswer(int frageId, user user) { using (umfrageDB db = new umfrageDB()) { if (!(from frage in db.questions where frage.Id == frageId select frage).Any()) return false; IQueryable<answer> lFragenLogs = from fragenLog in db.answers where fragenLog.UserId == user.AuthenticationId && fragenLog.FrageId == frageId select fragenLog; if (lFragenLogs.Any()) return false; } return true; }
public PairModel(question frage) { this.Id = frage.Id; this.FrageText = frage.FrageText; this.Category = frage.Type.Equals("student") ? "Schüler" : "Lehrer"; this.Informal = this.Category.Equals("Schüler"); using (umfrageDB db = new umfrageDB()) { this.Male = (from option in db.options where option.type.Equals(frage.Type) && option.sex.Equals('m') select new OptionModel(option)).ToArray() .OrderBy(model => this.Informal ? model.Name : model.Name.Split(' ')[1]); this.Female = (from option in db.options where option.type.Equals(frage.Type) && option.sex.Equals('f') select new OptionModel(option)).ToArray() .OrderBy(model => this.Informal ? model.Name : model.Name.Split(' ')[1]); } }
public static question FirstUnansweredQuestion(string userId) { using (umfrageDB db = new umfrageDB()) { int[] lAnsweredQuestions = (from user in db.users where user.Guid == userId join log in db.answers on user.AuthenticationId equals log.UserId select log.FrageId).ToArray(); int[] lQuestionIds = (from question in db.questions select question.Id).ToArray(); foreach ( IQueryable<question> lQuery in lQuestionIds.Where(i => !lAnsweredQuestions.Any(i1 => i1 == i)) .Select(i => (from frage in db.questions where frage.Id == i select frage)) .Where(lQuery => lQuery.Any())) { return lQuery.First(); } } return null; }