コード例 #1
0
ファイル: UserMailer.cs プロジェクト: mharen/service-tracker
        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;
            }
        }
コード例 #2
0
ファイル: Repo.cs プロジェクト: mharen/service-tracker
 public DbEntityEntry<User> Entry(User entity)
 {
     HttpContext.Current.Cache.Remove("Users"); // invalidate cache
     return db.Entry<User>(entity);
 }
コード例 #3
0
ファイル: Repo.cs プロジェクト: mharen/service-tracker
 public User Remove(User entity)
 {
     HttpContext.Current.Cache.Remove("Users"); // invalidate cache
     Entry(entity).State = System.Data.EntityState.Deleted;
     return entity;
 }
コード例 #4
0
ファイル: Repo.cs プロジェクト: mharen/service-tracker
 public User Add(User entity)
 {
     HttpContext.Current.Cache.Remove("Users"); // invalidate cache
     return db.Users.Add(entity);
 }
コード例 #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);
        }
コード例 #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();
        }
コード例 #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);
     }
 }
コード例 #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);
        }