예제 #1
0
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                Person person;

                string personId = ( string )Page.RouteData.Values["PersonId"] ?? string.Empty;
                if ( string.IsNullOrEmpty( personId ) )
                    personId = Request.QueryString["PersonId"];

                PersonService personService = new PersonService();

                if ( !string.IsNullOrEmpty( personId ) )
                    person = personService.Get( Convert.ToInt32( personId ) );
                else
                {
                    person = new Person();
                    personService.Add( person, CurrentPersonId );
                }

                txtFirstName.Text = person.FirstName;
                txtNickName.Text = person.NickName;
                txtLastName.Text = person.LastName;
            }
        }
예제 #2
0
        protected void btnUpdate_Click( object sender, EventArgs e )
        {
            if ( Page.IsValid )
            {
                Person person;

                string personId = ( string )Page.RouteData.Values["PersonId"] ?? string.Empty;
                if ( string.IsNullOrEmpty( personId ) )
                    personId = Request.QueryString["PersonId"];

                PersonService personService = new PersonService();

                if ( !string.IsNullOrEmpty( personId ) )
                    person = personService.Get( Convert.ToInt32( personId ) );
                else
                {
                    person = new Person();
                    personService.Add( person, CurrentPersonId );
                }

                person.GivenName = txtFirstName.Text;
                person.NickName = txtNickName.Text;
                person.LastName = txtLastName.Text;
                if ( person.Guid == Guid.Empty )
                    personService.Add( person, CurrentPersonId );
                personService.Save( person, CurrentPersonId );
            }
        }
예제 #3
0
        protected void btnSend_Click( object sender, EventArgs e )
        {
            PersonService personService = new PersonService();

            var mergeObjects = new List<object>();

            var values = new Dictionary<string, string>();
            values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
            mergeObjects.Add( values );

            Dictionary<object, List<object>> personObjects = new Dictionary<object, List<object>>();

            foreach(Person person in personService.GetByEmail(tbEmail.Text))
            {
                var userObjects = new List<object>();

                UserService userService = new UserService();
                foreach ( User user in userService.GetByPersonId( person.Id ) )
                    if ( user.AuthenticationType != AuthenticationType.Facebook )
                        userObjects.Add( user );

                if ( userObjects.Count > 0 )
                    personObjects.Add( person, userObjects );
            }

            if ( personObjects.Count > 0 )
            {
                mergeObjects.Add( personObjects );

                var recipients = new Dictionary<string, List<object>>();
                recipients.Add( tbEmail.Text, mergeObjects );

                Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_FORGOT_USERNAME );
                SetSMTPParameters( email );
                email.Send( recipients );

                pnlEntry.Visible = false;
                pnlSuccess.Visible = true;
            }
            else
                pnlWarning.Visible = true;
        }
예제 #4
0
        private void DisplaySuccess( Rock.CMS.User user )
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.SetAuthCookie( tbUserName.Text, false );

            if ( user != null && user.PersonId.HasValue )
            {
                PersonService personService = new PersonService();
                Person person = personService.Get( user.PersonId.Value );

                if ( person != null )
                {
                    var mergeObjects = new List<object>();
                    mergeObjects.Add( person );
                    mergeObjects.Add( user );

                    var values = new Dictionary<string, string>();
                    values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
                    mergeObjects.Add( values );

                    var recipients = new Dictionary<string, List<object>>();
                    recipients.Add( person.Email, mergeObjects );

                    Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_ACCOUNT_CREATED );
                    SetSMTPParameters( email );
                    email.Send( recipients );

                    lSuccessCaption.Text = AttributeValue( "SuccessCaption" );
                    if ( lSuccessCaption.Text.Contains( "{0}" ) )
                        lSuccessCaption.Text = string.Format( lSuccessCaption.Text, person.FirstName );

                    ShowPanel( 5 );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }
            else
                ShowErrorMessage( "Invalid User" );
        }
예제 #5
0
        private void DisplaySentLogin( Direction direction )
        {
            using ( new Rock.Data.UnitOfWorkScope() )
            {
                PersonService personService = new PersonService();
                Rock.CMS.UserService userService = new Rock.CMS.UserService();

                Person person = personService.Get( Int32.Parse( hfSendPersonId.Value ) );
                if ( person != null )
                {
                    var mergeObjects = new List<object>();

                    var values = new Dictionary<string, string>();
                    values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
                    mergeObjects.Add( values );

                    Dictionary<object, List<object>> personObjects = new Dictionary<object, List<object>>();
                    var userObjects = new List<object>();

                    mergeObjects.Add( person );

                    foreach ( var user in userService.GetByPersonId( person.Id ) )
                        if (user.AuthenticationType != Rock.CMS.AuthenticationType.Facebook)
                            userObjects.Add( user );

                    personObjects.Add( person, userObjects );
                    mergeObjects.Add(personObjects);

                    var recipients = new Dictionary<string, List<object>>();
                    recipients.Add(person.Email, mergeObjects);

                    Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_FORGOT_USERNAME );
                    SetSMTPParameters( email );
                    email.Send( recipients );
                }
                else
                    ShowErrorMessage( "Invalid Person" );
            }

            ShowPanel( 3 );
        }
예제 #6
0
        private void DisplaySendLogin( int personId, Direction direction )
        {
            hfSendPersonId.Value = personId.ToString();

            lExistingAccountCaption.Text = AttributeValue( "ExistingAccountCaption" );
            if ( lExistingAccountCaption.Text.Contains( "{0}" ) )
            {
                PersonService personService = new PersonService();
                Person person = personService.Get( personId );
                if ( person != null )
                    lExistingAccountCaption.Text = string.Format( lExistingAccountCaption.Text, person.FirstName );
            }

            ShowPanel( 2 );
        }
예제 #7
0
        private void DisplayDuplicates( Direction direction )
        {
            bool displayed = false;

            if ( Convert.ToBoolean( AttributeValue( "Duplicates" ) ) )
            {
                PersonService personService = new PersonService();
                var matches = personService.
                    Queryable().
                    Where( p =>
                        p.Email.ToLower() == tbEmail.Text.ToLower() &&
                        p.LastName.ToLower() == tbLastName.Text.ToLower() ).
                    ToList();

                if ( matches.Count > 0 )
                {
                    gDuplicates.AllowPaging = false;
                    gDuplicates.ShowActionRow = false;

                    gDuplicates.DataSource = matches;
                    gDuplicates.DataBind();

                    ShowPanel( 1 );

                    displayed = true;
                }
                else
                    displayed = false;

            }

            if ( !displayed )
            {
                if ( direction == Direction.Forward )
                    DisplaySuccess( CreateUser (CreatePerson(), true));
                else
                    DisplayUserInfo( direction );
            }
        }
예제 #8
0
        private void DisplayConfirmation( int personId )
        {
            PersonService personService = new PersonService();
            Person person = personService.Get(personId);

            if (person != null)
            {
                Rock.CMS.User user = CreateUser( person, false );

                var mergeObjects = new List<object>();
                mergeObjects.Add( person );
                mergeObjects.Add( user );

                var values = new Dictionary<string, string>();
                values.Add( "ConfirmAccountUrl", RootPath + "ConfirmAccount" );
                mergeObjects.Add( values );

                var recipients = new Dictionary<string, List<object>>();
                recipients.Add( person.Email, mergeObjects );

                Email email = new Email( Rock.SystemGuid.EmailTemplate.SECURITY_CONFIRM_ACCOUNT );
                SetSMTPParameters( email );
                email.Send( recipients );

                ShowPanel( 4 );
            }
            else
                ShowErrorMessage("Invalid Person");
        }
예제 #9
0
        private Person CreatePerson()
        {
            Rock.CRM.PersonService personService = new PersonService();

            Person person = new Person();
            person.GivenName = tbFirstName.Text;
            person.LastName = tbLastName.Text;
            person.Email = tbEmail.Text;
            switch(ddlGender.SelectedValue)
            {
                case "M":
                    person.Gender = Gender.Male;
                    break;
                case "F":
                    person.Gender = Gender.Female;
                    break;
                default:
                    person.Gender = Gender.Unknown;
                    break;
            }

            if (ddlBirthMonth.SelectedValue != "0")
                person.BirthMonth = Int32.Parse(ddlBirthMonth.SelectedValue);

            if (ddlBirthDay.SelectedValue != "0")
                person.BirthDay = Int32.Parse(ddlBirthDay.SelectedValue);

            if (ddlBirthYear.SelectedValue != "0")
                person.BirthYear = Int32.Parse(ddlBirthYear.SelectedValue);

            personService.Add(person, CurrentPersonId);
            personService.Save(person, CurrentPersonId);

            return person;
        }
예제 #10
0
        /// <summary>
        /// Awaits permission of facebook user and will issue authenication cookie if successful.
        /// </summary>
        /// <param name="code">Facebook authorization code</param>
        /// <param name="state">Redirect url</param>
        private void ProcessOAuth( string code, string state )
        {
            FacebookOAuthResult oAuthResult;

            if ( FacebookOAuthResult.TryParse( Request.Url, out oAuthResult ) && oAuthResult.IsSuccess )
            {
                try
                {
                    // create client to read response
                    var oAuthClient = new FacebookOAuthClient( FacebookApplication.Current ) { RedirectUri = new Uri( GetOAuthRedirectUrl() ) };
                    oAuthClient.AppId = PageInstance.Site.FacebookAppId;
                    oAuthClient.AppSecret = PageInstance.Site.FacebookAppSecret;
                    dynamic tokenResult = oAuthClient.ExchangeCodeForAccessToken( code );
                    string accessToken = tokenResult.access_token;

                    FacebookClient fbClient = new FacebookClient( accessToken );
                    dynamic me = fbClient.Get( "me" );
                    string facebookId = "FACEBOOK_" + me.id.ToString();

                    // query for matching id in the user table
                    UserService userService = new UserService();
                    var user = userService.GetByUserName( facebookId );

                    // if not user was found see if we can find a match in the person table
                    if ( user == null )
                    {
                        try
                        {
                            // determine if we can find a match and if so add an user login record

                            // get properties from Facebook dynamic object
                            string lastName = me.last_name.ToString();
                            string firstName = me.first_name.ToString();
                            string email = me.email.ToString();

                            var personService = new PersonService();
                            var person = personService.Queryable().FirstOrDefault( u => u.LastName == lastName && (u.GivenName == firstName || u.NickName == firstName) && u.Email == email );

                            if ( person != null )
                            {
                                // since we have the data enter the birthday from Facebook to the db if we don't have it yet
                                DateTime birthdate = Convert.ToDateTime( me.birthday.ToString() );

                                if ( person.BirthDay == null )
                                {
                                    person.BirthDate = birthdate;
                                    personService.Save( person, person.Id );
                                }

                            }
                            else
                            {
                                person = new Person();
                                person.GivenName = me.first_name.ToString();
                                person.LastName = me.last_name.ToString();
                                person.Email = me.email.ToString();

                                if (me.gender.ToString() == "male")
                                    person.Gender = Gender.Male;
                                if (me.gender.ToString() == "female")
                                    person.Gender = Gender.Female;

                                person.BirthDate = Convert.ToDateTime( me.birthday.ToString() );

                                personService.Add( person, null );
                                personService.Save( person, null );
                            }

                            user = userService.Create( person, AuthenticationType.Facebook, facebookId, "fb", true, person.Id );
                        }
                        catch ( Exception ex )
                        {
                            string msg = ex.Message;
                            // TODO: probably should report something...
                        }

                        // TODO: Show label indicating inability to find user corresponding to facebook id
                    }

                    // update user record noting the login datetime
                    user.LastLoginDate = DateTime.Now;
                    user.LastActivityDate = DateTime.Now;
                    userService.Save( user, user.PersonId );

                    FormsAuthentication.SetAuthCookie( user.UserName, false );

                    if ( state != null )
                        Response.Redirect( state );

                }
                catch ( FacebookOAuthException oae )
                {
                    string msg = oae.Message;
                    // TODO: Add error handeling
                    // Error validating verification code. (usually from wrong return url very picky with formatting)
                    // Error validating client secret.
                    // Error validating application.
                }
            }
        }