protected override void InternalProcessRequest(HttpContext context) { Reminder2DataContext dataCtx = new Reminder2DataContext(); var query = from Task t in dataCtx.Tasks where t.idTask == TaskID select new { Task = t, UserName = t.User.UserName }; var result = query.FirstOrDefault(); if (result != null) try { if (result.UserName.Equals(context.User.Identity.Name, StringComparison.OrdinalIgnoreCase)) { result.Task.Checked = true; dataCtx.SubmitChanges(); WriteOutputStatus(CommonStatusCode.OK, "OK."); } else WriteOutputStatus(CommonStatusCode.Forbidden, "You are not allowed to mark this task as checked."); } catch (Exception ex) { WriteOutputStatus(CommonStatusCode.ServerError, string.Format("Couldn't mark the task as checked. {0}", ex.Message)); } else WriteOutputStatus(CommonStatusCode.NotFound, "Task not found."); }
/// <summary> /// Processes the HTTP request. /// </summary> /// <param name="context">Context of the HTTP operation.</param> protected override void InternalProcessRequest(HttpContext context) { Reminder2DataContext dataCtx = new Reminder2DataContext(); var query = from User u in dataCtx.Users where u.UserName == context.User.Identity.Name select u.idUser; if (query.Count() > 0) try { Task task = new Task() { Message = this.Message, DateTime = this.DateTime, idUser = query.First() }; dataCtx.Tasks.InsertOnSubmit(task); dataCtx.SubmitChanges(); WriteOutputStatus(200, "OK."); XmlOutput.WriteStartElement("task"); try { XmlOutput.WriteAttributeString("id", task.idTask.ToString()); } finally { XmlOutput.WriteEndElement(); } } catch (Exception ex) { WriteOutputStatus(CommonStatusCode.ServerError, string.Format("Couldn't add task. {0}", ex.Message)); } else WriteOutputStatus(CommonStatusCode.ServerError, "Invalid user Id."); }
/// <summary> /// Updates an user's data in the database. /// </summary> /// <param name="user">The user to be updated.</param> public override void UpdateUser(MembershipUser user) { Rem2MembershipUser memUser = user as Rem2MembershipUser; if (memUser == null) throw new ArgumentException("The supplied Membership user is not compatible with the DB."); Reminder2DataContext ctx = new Reminder2DataContext(); var query = from User u in ctx.Users where u.UserName == user.UserName select u; User found = query.FirstOrDefault(); if (found == null) return; found.Email = memUser.Email; found.FirstName = memUser.FirstName; found.LastName = memUser.LastName; ctx.SubmitChanges(); }
/// <summary> /// Changes password for an user. /// </summary> /// <param name="username">Name of the user.</param> /// <param name="oldPassword">Old password.</param> /// <param name="newPassword">New password.</param> /// <returns>True if the operation was successful, false otherwise.</returns> public override bool ChangePassword(string username, string oldPassword, string newPassword) { Reminder2DataContext ctx = new Reminder2DataContext(); var query = from User u in ctx.Users where (u.UserName == username) && (u.Password == oldPassword) select u; User found = query.FirstOrDefault(); if (found == null) return false; try { found.Password = newPassword; ctx.SubmitChanges(); return true; } catch { return false; } }
/// <summary> /// Creates a new user and inserts him into the DB. /// </summary> /// <param name="userName">Name of the user.</param> /// <param name="password">User password.</param> /// <param name="email">E-mail address.</param> /// <param name="firstName">First name.</param> /// <param name="lastName">Last name.</param> /// <param name="status">User creation status.</param> /// <returns>A new Membership user if successful, null otherwise.</returns> public Rem2MembershipUser CreateUser(string userName, string password, string email, string firstName, string lastName, out MembershipCreateStatus status) { Reminder2DataContext ctx = new Reminder2DataContext(); //check user name if (string.IsNullOrEmpty(userName)) { status = MembershipCreateStatus.InvalidUserName; return null; } //check if the user already exists if (ctx.Users.FirstOrDefault(u => u.UserName == userName) != null) { status = MembershipCreateStatus.DuplicateUserName; return null; } Rem2MembershipUser memUser = null; try { User dbUser = new User() { UserName = userName, Password = password, Email = email, FirstName = firstName, LastName = lastName }; ctx.Users.InsertOnSubmit(dbUser); ctx.SubmitChanges(); memUser = new Rem2MembershipUser(this.Name, dbUser); } catch { status = MembershipCreateStatus.ProviderError; return null; } status = MembershipCreateStatus.Success; return memUser; }