Пример #1
0
        /// <summary>
        /// Sends an email to the recipient inviting them an Assessment.
        /// The content for this email is not defined by a template on the back end because the
        /// user might have customized the subject and/or body in the browser.
        ///
        /// See class level note (top of the file)
        /// </summary>
        public void InviteToAssessment(ContactCreateParameters contact)
        {
            string bodyHtml = ResourceHelper.GetEmbeddedResource(@"App_Data\assessmentInviteTemplate_" + this.Scope + ".html");

            // Build the name if supplied.
            string contactName = string.Empty;

            if (!string.IsNullOrEmpty(contact.FirstName) || !string.IsNullOrEmpty(contact.FirstName))
            {
                contactName = (contact.FirstName + " " + contact.LastName).Trim() + ",";
            }

            bodyHtml = bodyHtml.Replace("{{name}}", contactName);
            bodyHtml = bodyHtml.Replace("{{subject}}", contact.Subject);
            bodyHtml = bodyHtml.Replace("{{body}}", contact.Body.Replace("\r\n", "<br/>").Replace("\r", "<br/>").Replace("\n", "<br/>"));
            bodyHtml = bodyHtml.Replace("{{id}}", contact.AssessmentId.ToString());
            bodyHtml = bodyHtml.Replace("{{rootUrl}}", Utilities.GetClientHost());

            MailMessage message = new MailMessage();

            message.Subject    = contact.Subject;
            message.Body       = bodyHtml;
            message.IsBodyHtml = true;
            message.To.Add(new MailAddress(contact.PrimaryEmail));
            message.From = new MailAddress(
                ConfigurationManager.GetAppSetting("Sender Email"),
                ConfigurationManager.GetAppSetting("Sender Display Name"));
            SendMail(message);
        }
Пример #2
0
        public ContactsListResponse CreateAndAddContactToAssessment([FromBody] ContactCreateParameters newContact)
        {
            int          assessmentId = Auth.AssessmentForUser();
            TokenManager tm           = new TokenManager();
            string       app_code     = tm.Payload(Constants.Token_Scope);

            // Make sure the user is an admin on this assessment
            Auth.AuthorizeAdminRole();

            newContact.AssessmentId = assessmentId;
            newContact.PrimaryEmail = newContact.PrimaryEmail ?? "";

            ContactsManager      cm      = new ContactsManager();
            List <ContactDetail> details = new List <ContactDetail>(1);

            details.Add(cm.CreateAndAddContactToAssessment(newContact));

            ContactsListResponse resp = new ContactsListResponse
            {
                ContactList     = details,
                CurrentUserRole = cm.GetUserRoleOnAssessment(TransactionSecurity.CurrentUserId, assessmentId) ?? 0
            };

            return(resp);
        }
Пример #3
0
        /// <summary>
        /// Creates or updates the rows necessary to attach a Contact to an Assessment.
        /// Creates a new User based on email.
        /// Creates or overwrites the ASSESSMENT_CONTACTS connection.
        /// Creates a new Contact if needed.  If a Contact already exists for the email, no
        /// changes are made to the Contact row.
        /// </summary>
        public ContactDetail CreateAndAddContactToAssessment(ContactCreateParameters newContact)
        {
            int          assessmentId = Auth.AssessmentForUser();
            TokenManager tm           = new TokenManager();
            string       app_code     = tm.Payload(Constants.Token_Scope);

            NotificationManager nm = new NotificationManager();

            ASSESSMENT_CONTACTS existingContact = null;

            using (var db = new CSET_Context())
            {
                // See if the Contact already exists
                existingContact = db.ASSESSMENT_CONTACTS.Where(x => x.UserId == newContact.UserId && x.Assessment_Id == assessmentId).FirstOrDefault();
                if (existingContact == null)
                {
                    // Create Contact
                    var c = new ASSESSMENT_CONTACTS()
                    {
                        FirstName        = newContact.FirstName,
                        LastName         = newContact.LastName,
                        PrimaryEmail     = newContact.PrimaryEmail,
                        Assessment_Id    = assessmentId,
                        AssessmentRoleId = newContact.AssessmentRoleId,
                        Title            = newContact.Title,
                        Phone            = newContact.Phone
                    };

                    // Include the userid if such a user exists
                    USERS user = db.USERS.Where(u => !string.IsNullOrEmpty(u.PrimaryEmail) &&
                                                u.PrimaryEmail == newContact.PrimaryEmail)
                                 .FirstOrDefault();
                    if (user != null)
                    {
                        c.UserId = user.UserId;
                    }

                    db.ASSESSMENT_CONTACTS.AddOrUpdate(c, x => x.Assessment_Contact_Id);


                    // If there was no USER record for this new Contact, create one
                    if (user == null)
                    {
                        UserDetail userDetail = new UserDetail
                        {
                            Email                 = newContact.PrimaryEmail,
                            FirstName             = newContact.FirstName,
                            LastName              = newContact.LastName,
                            IsSuperUser           = false,
                            PasswordResetRequired = true
                        };
                        UserManager        um   = new UserManager();
                        UserCreateResponse resp = um.CheckUserExists(userDetail);
                        if (!resp.IsExisting)
                        {
                            resp = um.CreateUser(userDetail);

                            // Send this brand-new user an email with their temporary password (if they have an email)
                            if (!string.IsNullOrEmpty(userDetail.Email))
                            {
                                if (!UserAuthentication.IsLocalInstallation(app_code))
                                {
                                    nm.SendInviteePassword(userDetail.Email, userDetail.FirstName, userDetail.LastName, resp.TemporaryPassword);
                                }
                            }
                        }
                        c.UserId = resp.UserId;
                    }

                    db.SaveChanges();

                    AssessmentUtil.TouchAssessment(assessmentId);

                    existingContact = c;
                }
            }

            // Flip the 'invite' flag to true, if they are a contact on the current Assessment
            ContactsManager cm = new ContactsManager();

            cm.MarkContactInvited(newContact.UserId, assessmentId);

            // Tell the user that they have been invited to participate in an Assessment (if they have an email)
            if (!string.IsNullOrEmpty(newContact.PrimaryEmail))
            {
                if (!UserAuthentication.IsLocalInstallation(app_code))
                {
                    nm.InviteToAssessment(newContact);
                }
            }

            // Return the full list of Contacts for the Assessment
            return(new ContactDetail
            {
                FirstName = existingContact.FirstName,
                LastName = existingContact.LastName,
                PrimaryEmail = existingContact.PrimaryEmail,
                AssessmentId = existingContact.Assessment_Id,
                AssessmentRoleId = existingContact.AssessmentRoleId,
                Invited = existingContact.Invited,
                UserId = existingContact.UserId ?? null,
                Title = existingContact.Title,
                Phone = existingContact.Phone
            });
        }