/// <summary> /// Adds a new user /// </summary> /// <param name="form">User data</param> /// <returns>success</returns> public bool add(Form_User_Add form) { user newUser = new user(); newUser.username = form["username"].getValue(); newUser.rolesid = long.Parse(form["role"].getValue()); newUser.email = form["email"].getValue(); SHA1Managed sha = new SHA1Managed(); string hash = BitConverter.ToString(sha.ComputeHash(Encoding.Default.GetBytes(form["password"].getValue()))).Replace("-", String.Empty).ToLower(); newUser.password = hash; newUser.date = DateTime.Now; using (UserDataContext u = new UserDataContext()) { try { u.users.InsertOnSubmit(newUser); u.SubmitChanges(); } catch (Exception e) { CMS.Services.CMS_Services_Message.getInstance().addError(e.Message); return false; } } return true; }
/// <summary> /// Deletes a user with the given id /// </summary> /// <param name="id">user id</param> /// <returns>success</returns> public bool delete(long id) { using (UserDataContext u = new UserDataContext()) { try { u.users.DeleteAllOnSubmit(u.users.Where(x => x.id == id)); u.SubmitChanges(); } catch { return false; } return true; } }
/// <summary> /// Save changes to the given user /// </summary> /// <param name="form">User data</param> /// <param name="edited">Edited user</param> /// <returns>success</returns> public bool save(Form_User_Add form, user edited) { user toSave = new user(); toSave.id = edited.id; toSave.password = edited.password; if (form["password"].getValue() != String.Empty) { SHA1Managed sha = new SHA1Managed(); string hash = BitConverter.ToString(sha.ComputeHash(Encoding.Default.GetBytes(form["password"].getValue()))).Replace("-", String.Empty).ToLower(); toSave.password = hash; } toSave.rolesid = long.Parse(form["role"].getValue()); toSave.email = form["email"].getValue(); toSave.username = form["username"].getValue(); toSave.date = edited.date; using (UserDataContext u = new UserDataContext()) { u.users.Attach(toSave, edited); try { u.SubmitChanges(); } catch (Exception e) { return false; } } return true; }