// Personal Webpage Engine
// Copyright(c) 2008 Mihail Kochanov
// You can contact me at StilgarAD@yahoo.com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
//
// Personal Web Page Engine
//
//------------------------------------------------------------------------------

using System;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls;

public partial class Registration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InvisibleCaptcha.InititializeCaptcha(this, ViewState);
    }
    protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
    {
        InvisibleCaptcha.IsCaptchaValid(this, ViewState, true);
    }
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        string adminUsername = ConfigurationManager.AppSettings["AdminUsername"];
        string autoApprove = ConfigurationManager.AppSettings["AutoApproveNewUser"];

        //consider setting the DisableCreatedUser property of the CreateUserWizard
        if (autoApprove == "False")
        {
            MembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName);
            newUser.IsApproved = false;
            Membership.UpdateUser(newUser);
        }

        if(!String.IsNullOrEmpty(adminUsername))
        {
            if (CreateUserWizard1.UserName == adminUsername)
            {
                MembershipUser adminUser = Membership.GetUser(adminUsername);

                adminUser.IsApproved = true;

                if (!Roles.RoleExists(Constants.Roles.Admin))
                {
                    Roles.CreateRole(Constants.Roles.Admin);
                }

                Roles.AddUserToRole(adminUsername, Constants.Roles.Admin);

                Membership.UpdateUser(adminUser);
            }
        }

        //clear user list cache so new users are displayed in the user management
        Cache.Remove(Constants.Cache.Users);
    }
}
