public LoginModule(PeopleContext people, Ldap ldap) { _people = people; _ldap = ldap; Get["/login"] = parameters => { // Called when the user visits the login page or is redirected here because // an attempt was made to access a restricted resource. It should return // the view that contains the login form return View["login"]; }; Get["/logoff"] = parameters => { // Called when the user clicks the sign out button in the application. Should // perform one of the Logout actions (see below) return this.LogoutAndRedirect("/"); }; Post["/login"] = parameters => { // Called when the user submits the contents of the login form. Should // validate the user based on the posted form data, and perform one of the // Login actions (see below) var username = (string) Request.Form.username; var password = (string) Request.Form.password; // Authenticate user against AD if (!_ldap.IsAuthenticated(ConfigurationManager.AppSettings.Get("ldap-domain"), username, password)) { return View["login", "Unable to validate your account. Please contact the dev team at [email protected]"]; } var user = _people.People.FirstOrDefault(p => p.AdUser == username && !p.Hidden && !p.Retired); if (user == null) { // User was not found in the database, register the ad user. var newUser = ldap.GetUser(username); user = new Person { Id = Guid.NewGuid(), AdUser = username, Created = DateTime.Now, Email = newUser.Properties["mail"][0].ToString(), Name = newUser.Properties["displayName"][0].ToString() }; user = _people.People.Add(user); _people.SaveChanges(); } return this.LoginAndRedirect(user.Id, null, "/profile"); }; }
public ProfileModule(PeopleContext people) { this.RequiresAuthentication(); _people = people; Get["/profile"] = parameters => { // call when user visit it's own profile var identity = Context.CurrentUser as FloreamIdentity; var user = _people.People.FirstOrDefault(p => p.AdUser == identity.UserName); return View["profile", user]; }; Post["/profile/upload"] = parameters => { var file = Request.Files.FirstOrDefault(); if (file == null) { return new Response().WithStatusCode(HttpStatusCode.BadRequest); } var identity = Context.CurrentUser as FloreamIdentity; var imageType = file.ContentType.Split('/')[1]; var imageHeight = int.Parse(ConfigurationManager.AppSettings.Get("profile-image-height")); var imageWidth = int.Parse(ConfigurationManager.AppSettings.Get("profile-image-width")); var memStream = new MemoryStream(); var img = Image.FromStream(file.Value); if (img.Height > imageHeight || img.Width > imageWidth) { // Resize the image var bmp = ScaleImage(img, imageWidth, imageHeight); // Save the resized image to a stream var imageFormatConverter = new ImageFormatConverter(); var imageObj = imageFormatConverter.ConvertFromString(imageType); if (imageObj != null) { bmp.Save(memStream, (ImageFormat) imageObj); } } else { img.Save(memStream, img.RawFormat); } var array = memStream.ToArray(); // Update the user's profile var user = _people.People.FirstOrDefault(p => p.AdUser == identity.UserName); if (user != null) { user.PictureExtension = imageType; user.Picture = array; _people.SaveChanges(); } return Response.AsText(HtmlHelper.GetProfileImage(array, imageType)); }; }