Inheritance: DbContext
Example #1
0
        public virtual User AcceptApplication()
        {
            User user = null;
              using (var session = new Session()) {
            //set the status
            CurrentApplication.Status = ApplicationStatus.Accepted;

            //crete the new user
            user = CreateUserFromCurrentApplication();

            //log the registration
            user.AddLogEntry("Registration", "User with email " + user.Email + " successfully registered");

            //send off an email
            var mailer = session.Mailers.FirstOrDefault(x => x.MailerType == MailerType.EmailConfirmation);
            if (mailer != null && _config.RequireEmailConfirmation) {
              //TODO need to hook this up so the email can be confirmed
              var message = UserMailerMessage.CreateFromTemplate(mailer, _config.ConfirmationUrl + "?t="+user.AuthenticationToken);
              message.SendTo(user);
            }

            user.AddLogEntry("Registration", "Email confirmation request sent");

            //save the user down
            session.Users.Add(user);
            session.SaveChanges();
              }
              return user;
        }
Example #2
0
        public TokenResponseExpired()
        {
            var app = new Application("*****@*****.**", "password", "password");
              var regResult = new Registrator().ApplyForMembership(app);

              using (var session = new Session()) {
            var user = session.Users.FirstOrDefault(x => x.Email == regResult.NewUser.Email);
            user.ReminderSentAt = DateTime.Now.AddDays(-1);
            session.SaveChanges();
              }

              result = new MonkeyFist.Services.Reminders().ResetUserPassword(regResult.NewUser.ReminderToken, "newPassword");
        }
Example #3
0
        static void Main(string[] args)
        {
            var session = new Session();
            //var user = new User { Email="*****@*****.**"};
            //Console.WriteLine(user.ID);

            foreach (var log in session.UserActivityLogs)
            {
                Console.WriteLine(log.Data);
            }

            //session.Users.Add(user);
            //session.SaveChanges();

            Console.WriteLine("Done!");
            Console.ReadLine();
        }
Example #4
0
        public AuthenticationResult AuthenticateUser(Credentials creds)
        {
            _session = new Session();
              var result = new AuthenticationResult();
              User user = null;
              this.CurrentCredentials = creds;

              if (EmailOrPasswordNotPresent()) {
            result = InvalidLogin(Properties.Resources.EmailOrPasswordMissing);
              } else {
            //find the user
            user = LocateUser();

            //if they're not here, we're done
            if (user == null) {
              result = InvalidLogin(Properties.Resources.InvalidLogin);

              //does the password match?
            } else if (HashedPasswordDoesNotMatch(user)) {
              result = InvalidLogin(Properties.Resources.InvalidLogin);

              //success
            } else {
              //success!
              user.AddLogEntry("Login", "User logged in");
              result.Session = CreateSession(user);

              SetUserLoginStats(user);
              //save changes
              UserAuthenticated(user);

              result.Authenticated = true;
              result.User = user;
              result.Message = Properties.Resources.UserAuthenticated;

              _session.SaveChanges();
            }
              }

              //dispose of this
              _session.Dispose();

              return result;
        }
Example #5
0
        public ResetResult ResetUserPassword(Guid token, string newPassword)
        {
            var result = new ResetResult();
              _session = _session ?? new Session();
              var user = GetUserByToken(token);
              if (user != null) {
            if (PasswordResetIsValid(newPassword)) {
              if (ResetWindowIsOpen(user)) {

            var hashed = BCryptHelper.HashPassword(newPassword, BCryptHelper.GenerateSalt(10));
            user.HashedPassword = hashed;
            user.AddLogEntry("Login", "Password was reset");
            _session.SaveChanges();
            result.Successful = true;
            result.Message = Properties.Resources.PasswordResetSuccessful;
            result.User = user;
              } else {
            result.Message = Properties.Resources.PasswordResetExpired;
              }
            } else {
              result.Message = Properties.Resources.InvalidPassword;
            }
              } else {
            result.Message = Properties.Resources.PasswordResetTokenInvalid;
              }
              _session.Dispose();
              return result;
        }
Example #6
0
        public ReminderResult SendReminderTokenToUser(string email)
        {
            _session = new Session();
              var result = new ReminderResult();
              result.User = GetUserByEmail(email);

              if (result.User != null) {
            result.User.ReminderToken = Guid.NewGuid();
            result.User.ReminderSentAt = DateTime.Now;

            var mailer = GetReminderMailer();
            var link = CreateReminderLink(result.User);
            var message = UserMailerMessage.CreateFromTemplate(mailer,link);

            if (message.Successful) {
              result.User.AddLogEntry("Login", "Reminder email sent at " + DateTime.Now.ToShortDateString());
            } else {
              result.User.AddLogEntry("Login", "Reminder email failed to send  " + DateTime.Now.ToShortDateString());
            }

            result.MailMessage = message.SendTo(result.User);

            _session.SaveChanges();
              } else {
            result.Message = Properties.Resources.EmailNotFound;
              }

              _session.Dispose();

              return result;
        }
Example #7
0
 public virtual bool EmailAlreadyRegistered()
 {
     var exists = false;
       using (var session = new Session()) {
     exists = session.Users.FirstOrDefault(x => x.Email == CurrentApplication.Email) != null;
       }
       return exists;
 }
Example #8
0
 public virtual User GetCurrentUser(Guid sessionToken)
 {
     User user = null;
       using (var session = new Session()) {
     var validSession = session.Sessions.Include("User").FirstOrDefault(x => x.ID == sessionToken && x.EndsAt > DateTime.Now);
     if (validSession != null) {
       user = validSession.User;
     }
       }
       return user;
 }
Example #9
0
 public virtual bool EndUserSession(Guid sessionToken)
 {
     var result = false;
       using (var session = new Session()) {
     var userSession = session.Sessions.FirstOrDefault(x => x.ID == sessionToken);
     if (userSession != null) {
       userSession.EndsAt = DateTime.Now;
       session.SaveChanges();
       result = true;
     }
       }
       return result;
 }
Example #10
0
        public AuthenticationResult AuthenticateUserByToken(string token, string ip = "127.0.0.1")
        {
            var result = new AuthenticationResult();
              _session = new Session();

              if (String.IsNullOrWhiteSpace(token)) {
            result = InvalidLogin("No token provided");
              } else {
            this.CurrentCredentials = new Credentials { Token = Guid.Parse(token), IP = ip };

            var user = FindUserByAuthenticationToken();
            if (user == null) {
              result = InvalidLogin("Invalid token");
            } else {
              //success
              user.AddLogEntry("Login", "User logged in by token");
              result.Session = CreateSession(user);
              SetUserLoginStats(user);
              UserAuthenticated(user);

              result.Authenticated = true;
              result.User = user;
              result.Message = Properties.Resources.UserAuthenticated;
              _session.SaveChanges();
            }
              }
              _session.Dispose();
              return result;
        }