public void Update(T item) { MPRSession session = MPRController.StartSession(); MPRController.Save <T>(session, item); MPRController.EndSession(session, true); }
protected void itemDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { // Because ObjectDataSource loses all the non-displayed values, as well as composite values, // we need to reload them here from the object. log.Debug(String.Format("Updating item {0}", e.Keys["Id"])); MPRSession session = MPRController.StartSession(); MPItem item = MPRController.RetrieveById <MPItem>(session, (Int64)e.Keys["Id"]); item.Name = (string)e.NewValues["Name"]; item.Description = (string)e.NewValues["Description"]; item.DescriptionShort = (string)e.NewValues["DescriptionShort"]; item.Author = (string)e.NewValues["Author"]; item.Homepage = (string)e.NewValues["Homepage"]; item.License = (string)e.NewValues["License"]; item.LicenseMustAccept = (bool)e.NewValues["LicenseMustAccept"]; item.Tags = MPRController.GetTags(session, ((TextBox)itemDetailsView.FindControl("tagsTextBox")).Text); MPRController.Save <MPItem>(session, item); MPRController.EndSession(session, true); log.Info(String.Format("Updated item {0} ({1})", e.Keys["Id"], e.NewValues["Name"])); // Manually reset the form to view format e.Cancel = true; itemDetailsView.ChangeMode(DetailsViewMode.ReadOnly); }
public void TestUserAdd() { MPRSession session = MPRController.StartSession(); MPUser user = new MPUser(); user.Name = "joe submitter"; user.EMail = "mp-test@localhost"; user.Handle = "submitter"; IList <MPUserPermission> permissions = session.Session .CreateQuery("from MPUserPermission perms where perms.Name in ('Download', 'Add')") .List <MPUserPermission>(); user.Permissions.AddAll(permissions); MPRController.Save <MPUser>(session, user); user = new MPUser(); user.Name = "james admin"; user.EMail = "mp-test@localhost"; user.Handle = "admin"; permissions = session.Session .CreateQuery("from MPUserPermission perms where perms.Name in ('Download', 'Add', 'Approve', 'Delete')") .List <MPUserPermission>(); user.Permissions.AddAll(permissions); MPRController.Save <MPUser>(session, user); MPRController.EndSession(session, true); }
public static MPUser GetCurrentUser() { MPUser user = (MPUser)HttpContext.Current.Session["MPUser"]; if (user != null) { return(user); } string handle = HttpContext.Current.User.Identity.Name; if (handle != null) { // user has been authenticated before (marked remember me) // TODO: move this logic to the PostAuthenticateRequest event handler MPRSession session = MPRController.StartSession(); IList <MPUser> users = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle); if (users.Count == 1) { user = users[0]; user.LastLogin = DateTime.Now; MPRController.Save <MPUser>(session, user); MPRController.EndSession(session, true); HttpContext.Current.Session["MPUser"] = user; return(user); } MPRController.EndSession(session, false); } throw new InvalidUserException("The session doesn't contain a valid user"); }
public Int64 Insert(T item) { MPRSession session = MPRController.StartSession(); Int64 newId; MPRController.Save <T>(session, item, out newId); MPRController.EndSession(session, true); return(newId); }
public void TestPermissionAdd() { string[,] permissions = { { "Download", "Can download files" }, { "Add", "Can upload files" }, { "Approve", "Allowed to approve awaiting items in the queue" }, { "Delete", "Allowed to delete items" }, }; MPRSession session = MPRController.StartSession(); for (int i = 0; i <= permissions.GetUpperBound(0); i++) { MPUserPermission permission = new MPUserPermission(); permission.Name = permissions[i, 0]; permission.Description = permissions[i, 1]; MPRController.Save <MPUserPermission>(session, permission); } MPRController.EndSession(session, true); }
protected void versionDataSource_Inserted(object sender, ObjectDataSourceStatusEventArgs e) { string filename = (string)ViewState["TargetFileName"]; string location = (string)ViewState["TargetFileLocation"]; MPRSession session = MPRController.StartSession(); MPItemVersion version = MPRController.RetrieveById <MPItemVersion>(session, (Int64)e.ReturnValue); // Add the new file to the Repository MPFile mpfile = new MPFile(); mpfile.ItemVersion = version; mpfile.Filename = filename; mpfile.Location = location; version.Files.Add(mpfile); MPRController.Save <MPItemVersion>(session, version); MPRController.EndSession(session, true); ViewState.Remove("TargetFileName"); ViewState.Remove("TargetFileLocation"); }
protected void versionDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { // Because ObjectDataSource loses all the non-displayed values, as well as composite values, // we need to reload them here from the object. log.Debug(String.Format("Updating version {0}", e.Keys["Id"])); MPRSession session = MPRController.StartSession(); MPItemVersion version = MPRController.RetrieveById <MPItemVersion>(session, (Int64)e.Keys["Id"]); version.Version = (string)e.NewValues["Version"]; version.MPVersionMin = (string)e.NewValues["MPVersionMin"]; version.MPVersionMax = (string)e.NewValues["MPVersionMax"]; version.ReleaseNotes = (string)e.NewValues["ReleaseNotes"]; version.UpdateDate = DateTime.Now; MPRController.Save <MPItemVersion>(session, version); MPRController.EndSession(session, true); log.Info(String.Format("Updated version {0} ({1})", e.Keys["Id"], e.NewValues["Version"])); // Manually reset the form to view format e.Cancel = true; versionDetailsView.ChangeMode(DetailsViewMode.ReadOnly); }
protected void commentAddButton_Click(object sender, EventArgs e) { if (commentAddTextBox.Text.Length == 0) { commentAddLabel.Text = "Please enter a comment"; commentAddLabel.Visible = true; return; } if (itemsGridView.SelectedRow == null) { throw new InvalidFormDataException("Comment can only be added to a selected item"); } MPRSession session = MPRController.StartSession(); MPItem item = MPRController.RetrieveById <MPItem>(session, (Int64)itemsGridView.SelectedValue); if (item == null) { throw new InvalidFormDataException("Asked to provide details for invalid item id"); } MPItemComment comment = new MPItemComment(); comment.User = SessionUtils.GetCurrentUser(); comment.Text = commentAddTextBox.Text; item.Comments.Add(comment); MPRController.Save <MPItem>(session, item); MPRController.EndSession(session, true); commentAddTextBox.Text = ""; commentAddLabel.Text = "Comment added. Thank you!"; commentAddLabel.Visible = true; }
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { e.Authenticated = false; // TODO: replace with real authentication if (Login1.Password != "password") { return; } // Add user to session string handle = Login1.UserName; MPRSession session = MPRController.StartSession(); IList <MPUser> users = MPRController.RetrieveEquals <MPUser>(session, "Handle", handle); if (users.Count != 1) // Either none or a serious error { MPRController.EndSession(session, false); return; } MPUser user = users[0]; user.LastLogin = DateTime.Now; MPRController.Save <MPUser>(session, user); HttpContext.Current.Session["MPUser"] = user; MPRController.EndSession(session, true); e.Authenticated = true; log.Info(String.Format("User {0} has logged in", user.Handle)); FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet); }
/// <summary> /// Handle the actual creation of the entity /// </summary> /// <param name="filename">the name of the local file</param> /// <returns>success or failure</returns> protected bool AddFileToRepository(string filename) { MPRSession session = MPRController.StartSession(); MPUser user = SessionUtils.GetCurrentUser(); MPItem item = new MPItem(); item.Name = titleTextBox.Text; Int64 typeId; if (!Int64.TryParse(typesList.SelectedValue, out typeId)) { return(UploadFail(String.Format("Invalid item type {0}", typesList.SelectedValue))); } item.Type = MPRController.RetrieveById <MPItemType>(session, typeId); if (item.Type == null) { return(UploadFail(String.Format("Unable to find item type {0} ({1})", typesList.SelectedItem, typeId))); } List <Int64> categoryIds = new List <Int64>(); foreach (ListItem categoryItem in categoriesList.Items) { if (categoryItem.Selected) { Int64 id; if (Int64.TryParse(categoryItem.Value, out id)) { categoryIds.Add(id); } } } IList <MPCategory> categories = MPRController.RetrieveByIdList <MPCategory>(session, categoryIds); foreach (MPCategory category in categories) { item.Categories.Add(category); } item.Description = descriptionTextBox.Text; item.DescriptionShort = descriptionShortTextBox.Text; item.License = licenseTextBox.Text; item.LicenseMustAccept = licenseMustAccessCheckBox.Checked; item.Author = authorTextBox.Text; item.Homepage = homepageTextbox.Text; item.Tags = MPRController.GetTags(session, tagsTextBox.Text); // create ItemVersion MPItemVersion itemVersion = new MPItemVersion(); itemVersion.Item = item; itemVersion.Uploader = user; itemVersion.DevelopmentStatus = (MPItemVersion.MPDevelopmentStatus)Enum.Parse(typeof(MPItemVersion.MPDevelopmentStatus), developmentStatusDropDownList.SelectedValue); itemVersion.MPVersionMin = mpVersionMinTextBox.Text; itemVersion.MPVersionMax = mpVersionMaxTextBox.Text; itemVersion.Version = versionTextBox.Text; MPFile mpfile = new MPFile(); mpfile.ItemVersion = itemVersion; mpfile.Filename = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName); mpfile.Location = filename; itemVersion.Files.Add(mpfile); item.Versions.Add(itemVersion); // Save item (and sub-items) to database try { MPRController.Save <MPItem>(session, item); MPRController.EndSession(session, true); } catch (Exception ex) { MPRController.EndSession(session, false); return(UploadFail("Unable to save item: " + ex.ToString())); } return(true); }