Example #1
0
        public virtual MailMessage Invitation(User user)
        {
            using (var repo = new Repo())
            {
                var profile = repo.Profiles.Single();

                var from = profile.EmailFromAddress ?? "noreply@domain";

                var mailMessage = new MailMessage(from, user.Email)
                {
                    Subject = "Invitation to Service Tracker"
                };

                var bcc = profile.EmailBccAddress;
                if (!string.IsNullOrEmpty(bcc))
                {
                    mailMessage.Bcc.Add(bcc);
                }

                ViewBag.User = user;
                PopulateBody(mailMessage, viewName: "Invitation");

                return mailMessage;
            }
        }
Example #2
0
 public DbEntityEntry<User> Entry(User entity)
 {
     HttpContext.Current.Cache.Remove("Users"); // invalidate cache
     return db.Entry<User>(entity);
 }
Example #3
0
 public User Remove(User entity)
 {
     HttpContext.Current.Cache.Remove("Users"); // invalidate cache
     Entry(entity).State = System.Data.EntityState.Deleted;
     return entity;
 }
Example #4
0
 public User Add(User entity)
 {
     HttpContext.Current.Cache.Remove("Users"); // invalidate cache
     return db.Users.Add(entity);
 }
Example #5
0
        private void SendEmailInvitation(User user)
        {
            IUserMailer mailer = new UserMailer();
            var message = mailer.Invitation(user);
            message.Send();

            var log = new InvitationLog()
            {
                UserId = user.UserId,
                Action = (int)InvitationAction.Sent,
                LogDate = DateTime.UtcNow
            };
            repo.Add(log);
        }
Example #6
0
        private void CreateOrUpdateDbUser(string claimedIdentifier, string email, string invitationCode)
        {
            // see if user already exists
            var ExistingUserByOpenId = repo.Users.SingleOrDefault(u => u.ClaimedIdentifier == claimedIdentifier);
            var ExistingUserByInvite = repo.Users.SingleOrDefault(u => u.InvitationCode == invitationCode);

            if (ExistingUserByOpenId != null && ExistingUserByInvite != null)
            {
                // the user that accepted the invite already had an account. Delete the invite
                repo.Remove(ExistingUserByInvite);
                ExistingUserByInvite = null;
            }

            var ExistingUser = ExistingUserByOpenId ?? ExistingUserByInvite;

            if (ExistingUser == null)
            {
                var NewUser = new User
                {
                    ClaimedIdentifier = claimedIdentifier,
                    Email = email,
                    FirstLogin = DateTime.UtcNow,
                    LastLogin = DateTime.UtcNow,
                    LoginCount = 1,
                    RoleId = (int)RoleType.Guest
                };

                repo.Add(NewUser);
            }
            else
            {
                if (ExistingUser.ClaimedIdentifier == null)
                {
                    ExistingUser.ClaimedIdentifier = claimedIdentifier;
                    ExistingUser.FirstLogin = DateTime.UtcNow;
                    ExistingUser.InvitationCode = null; // clear out code since it's been redeemed

                    var log = new InvitationLog()
                    {
                        UserId = ExistingUser.UserId,
                        Action = (int)InvitationAction.Accepted,
                        LogDate = DateTime.UtcNow
                    };
                    repo.Add(log);
                }
                ExistingUser.Email = email;
                ExistingUser.LoginCount++;
                ExistingUser.LastLogin = DateTime.UtcNow;
            }
            repo.SaveChanges();
        }
Example #7
0
 public ActionResult Edit(User user)
 {
     // note: we can't just save off the given "user" object because it doesn't have
     // all the values from the user row (login count, date, claimed id, etc.) and to
     // do it that way would blow those other fields out of the DB, which
     // would make you look pretty foolish, amirite?
     try
     {
         var existingUser = repo.Users.Single(u => u.UserId == user.UserId);
         repo.Entry(existingUser).State = System.Data.EntityState.Modified;
         existingUser.OrganizationId = user.OrganizationId;
         existingUser.RoleId = user.RoleId;
         existingUser.ServicerId = user.ServicerId;
         repo.SaveChanges();
         TempData["Message"] = "User Saved";
         return RedirectToAction("Index");
     }
     catch (Exception ex)
     {
         TempData["Message"] = "Error: " + ex.Message;
         ViewBag.Organizations = repo.Organizations.ToSelectListItems();
         ViewBag.Servicers = repo.Servicers.ToSelectListItems();
         return View(user);
     }
 }
Example #8
0
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                // generate a new invite code
                user.InvitationCode = Extensions.Utilities.GenerateKey();
                repo.Add(user);
                repo.SaveChanges();

                var LogEntry = new InvitationLog()
                {
                    Action = (int)InvitationAction.Created,
                    UserId = user.UserId,
                    LogDate = DateTime.UtcNow
                };

                repo.Add(LogEntry);

                try
                {
                    SendEmailInvitation(user);
                    TempData["Message"] = "Invitation Sent";
                }
                catch (Exception ex)
                {
                    TempData["Message"] = string.Format("Invitation created, but could not be sent :( ({0})", ex.Message);
                }

                repo.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Organizations = repo.Organizations.ToSelectListItems();
            ViewBag.Servicers = repo.Servicers.ToSelectListItems();
            return View(user);
        }