コード例 #1
0
        /// <summary>
        /// AutoCreateAccount will automatically create an account based on infomration found both in the web.config file
        /// and the database.
        /// </summary>
        /// <returns>MAccountProfile</returns>
        public static MAccountProfile AutoCreateAccount()
        {
            MAccountProfile mCurrentAccountProfile = AccountUtility.GetProfile("System");
            MAccountProfile mAccountProfileToSave  = new MAccountProfile();
            Logger          mLog = Logger.Instance();

            mAccountProfileToSave.Id = -1;
            bool   mSaveGroups = true;
            bool   mSaveRoles  = true;
            string mGroups     = ConfigSettings.RegistrationGroups;
            string mRoles      = ConfigSettings.RegistrationRoles;

            if (string.IsNullOrEmpty(mGroups))
            {
                mSaveGroups = false;
            }
            if (string.IsNullOrEmpty(mRoles))
            {
                mSaveRoles = false;
            }
            mAccountProfileToSave.Account       = AccountUtility.HttpContextUserName();
            mAccountProfileToSave.FirstName     = "Auto created";
            mAccountProfileToSave.MiddleName    = "";
            mAccountProfileToSave.LastName      = "Auto created";
            mAccountProfileToSave.PreferredName = "Auto created";
            mAccountProfileToSave.Email         = "*****@*****.**";
            mAccountProfileToSave.Location      = "Hawaii";
            mAccountProfileToSave.TimeZone      = -8;
            mAccountProfileToSave.AddedBy       = mCurrentAccountProfile.Id;
            mAccountProfileToSave.AddedDate     = DateTime.Now;
            mAccountProfileToSave.SetGroups(mGroups);
            mAccountProfileToSave.SetRoles(mRoles);
            mAccountProfileToSave.PasswordLastSet = DateTime.Now;
            mAccountProfileToSave.LastLogOn       = DateTime.Now;
            mAccountProfileToSave.Password        = CryptoUtility.Encrypt(ConfigSettings.RegistrationPassword, ConfigSettings.EncryptionType);
            mAccountProfileToSave.Status          = (int)SystemStatus.SetAccountDetails;
            MClientChoicesState    mClientChoiceState     = ClientChoicesUtility.GetClientChoicesState(ConfigSettings.RegistrationAccountChoicesAccount, true);
            MSecurityEntityProfile mSecurityEntityProfile = SecurityEntityUtility.GetProfile(ConfigSettings.RegistrationSecurityEntityId);

            mClientChoiceState.IsDirty = false;
            mClientChoiceState[MClientChoices.AccountName]        = mAccountProfileToSave.Account;
            mClientChoiceState[MClientChoices.SecurityEntityId]   = mSecurityEntityProfile.Id.ToString(CultureInfo.InvariantCulture);
            mClientChoiceState[MClientChoices.SecurityEntityName] = mSecurityEntityProfile.Name;
            try
            {
                AccountUtility.Save(mAccountProfileToSave, mSaveRoles, mSaveGroups, mSecurityEntityProfile);
                ClientChoicesUtility.Save(mClientChoiceState, false);
                AccountUtility.SetPrincipal(mAccountProfileToSave);
            }
            catch (Exception ex)
            {
                mLog.Error(ex);
                throw;
            }
            return(mAccountProfileToSave);
        }
コード例 #2
0
        public IHttpActionResult Save(UIAccountProfile uiProfile)
        {
            if (uiProfile == null)
            {
                throw new ArgumentNullException("uiProfile", "uiProfile cannot be a null reference (Nothing in Visual Basic)!");
            }
            string          mRetVal                = "false";
            bool            mSaveGroups            = false;
            bool            mSaveRoles             = false;
            MAccountProfile mCurrentAccountProfile = AccountUtility.CurrentProfile();
            MAccountProfile mAccountProfileToSave  = new MAccountProfile();
            Logger          mLog = Logger.Instance();

            if (HttpContext.Current.Request.QueryString["Action"].ToString().ToUpper(CultureInfo.InvariantCulture).IndexOf("REGISTER") > -1)
            {
                MAccountProfile mExistingAccount = AccountUtility.GetProfile(uiProfile.Account);
                if (mExistingAccount == null)
                {
                    mAccountProfileToSave    = populateAccountProfile(uiProfile, mAccountProfileToSave);
                    mAccountProfileToSave.Id = uiProfile.Id;
                    string mGroups = ConfigSettings.RegistrationGroups;
                    string mRoles  = ConfigSettings.RegistrationRoles;
                    if (!string.IsNullOrEmpty(mGroups))
                    {
                        mSaveGroups = true;
                    }
                    if (!string.IsNullOrEmpty(mRoles))
                    {
                        mSaveRoles = true;
                    }
                    mAccountProfileToSave.AddedBy   = mCurrentAccountProfile.Id;
                    mAccountProfileToSave.AddedDate = DateTime.Now;
                    mAccountProfileToSave.SetGroups(mGroups);
                    mAccountProfileToSave.SetRoles(mRoles);
                    mAccountProfileToSave.PasswordLastSet = DateTime.Now;
                    mAccountProfileToSave.LastLogOn       = DateTime.Now;
                    mAccountProfileToSave.Password        = CryptoUtility.Encrypt(ConfigSettings.RegistrationPassword, ConfigSettings.EncryptionType);
                    mAccountProfileToSave.Status          = int.Parse(ConfigSettings.RegistrationStatusId);
                    if (HttpContext.Current.Request.QueryString["Action"].ToString().ToUpper(CultureInfo.InvariantCulture).IndexOf("REGISTER") > -1)
                    {
                        mAccountProfileToSave.Status = (int)SystemStatus.Active;
                    }
                    MClientChoicesState    mClientChoiceState     = ClientChoicesUtility.GetClientChoicesState(ConfigSettings.RegistrationAccountChoicesAccount, true);
                    MSecurityEntityProfile mSecurityEntityProfile = SecurityEntityUtility.GetProfile(ConfigSettings.RegistrationSecurityEntityId);
                    string mCurrentSecurityEntityId = mClientChoiceState[MClientChoices.SecurityEntityId];

                    mClientChoiceState.IsDirty = false;
                    mClientChoiceState[MClientChoices.AccountName]        = mAccountProfileToSave.Account;
                    mClientChoiceState[MClientChoices.SecurityEntityId]   = mSecurityEntityProfile.Id.ToString(CultureInfo.InvariantCulture);
                    mClientChoiceState[MClientChoices.SecurityEntityName] = mSecurityEntityProfile.Name;
                    try
                    {
                        AccountUtility.Save(mAccountProfileToSave, mSaveRoles, mSaveGroups, mSecurityEntityProfile);
                        ClientChoicesUtility.Save(mClientChoiceState, false);
                        AccountUtility.SetPrincipal(mAccountProfileToSave);
                        mRetVal = "Your account has been created";
                    }
                    catch (Exception ex)
                    {
                        mLog.Error(ex);
                    }
                }
                else
                {
                    mRetVal = "The account '" + uiProfile.Account + "' already exists please choose a different account/email";
                }
            }
            else
            {
                if ((HttpContext.Current.Items["EditId"] != null) | mCurrentAccountProfile.Status == (int)SystemStatus.SetAccountDetails)
                {
                    int mEditId = 0;
                    if ((HttpContext.Current.Items["EditId"] != null))
                    {
                        mEditId = int.Parse(HttpContext.Current.Items["EditId"].ToString());
                    }
                    else
                    {
                        mEditId = mCurrentAccountProfile.Id;
                    }
                    if (mEditId == uiProfile.Id)
                    {
                        MSecurityInfo mSecurityInfo = new MSecurityInfo(FunctionUtility.CurrentProfile(), AccountUtility.CurrentProfile());
                        if ((mSecurityInfo != null))
                        {
                            if (mEditId != -1)
                            {
                                if (mCurrentAccountProfile.Id != uiProfile.Id)
                                {
                                    mSecurityInfo = new MSecurityInfo(FunctionUtility.GetProfile(ConfigSettings.GetAppSettingValue("Actions_EditOtherAccount", true)), mCurrentAccountProfile);
                                }
                                if (mSecurityInfo.MayEdit | mCurrentAccountProfile.Status == (int)SystemStatus.SetAccountDetails)
                                {
                                    MSecurityInfo mGroupTabSecurity = new MSecurityInfo(FunctionUtility.GetProfile(ConfigSettings.GetAppSettingValue("Actions_View_Account_Group_Tab", true)), mCurrentAccountProfile);
                                    MSecurityInfo mRoleTabSecurity  = new MSecurityInfo(FunctionUtility.GetProfile(ConfigSettings.GetAppSettingValue("Actions_View_Account_Role_Tab", true)), mCurrentAccountProfile);
                                    mAccountProfileToSave    = AccountUtility.GetProfile(mEditId);
                                    mAccountProfileToSave    = populateAccountProfile(uiProfile, mAccountProfileToSave);
                                    mAccountProfileToSave.Id = uiProfile.Id;
                                    string mGroups = string.Join(",", uiProfile.AccountGroups.Groups);
                                    string mRoles  = string.Join(",", uiProfile.AccountRoles.Roles);
                                    if (mGroupTabSecurity.MayView & FunctionUtility.CurrentProfile().Action.ToLowerInvariant() == ConfigSettings.GetAppSettingValue("Actions_EditOtherAccount", true).ToLower(CultureInfo.InvariantCulture))
                                    {
                                        if (mAccountProfileToSave.GetCommaSeparatedAssignedGroups != mGroups)
                                        {
                                            mSaveGroups = true;
                                            mAccountProfileToSave.SetGroups(mGroups);
                                        }
                                    }
                                    if (mRoleTabSecurity.MayView & FunctionUtility.CurrentProfile().Action.ToLowerInvariant() == ConfigSettings.GetAppSettingValue("Actions_EditOtherAccount", true).ToLower(CultureInfo.InvariantCulture))
                                    {
                                        if (mAccountProfileToSave.GetCommaSeparatedAssignedRoles != mRoles)
                                        {
                                            mSaveRoles = true;
                                            mAccountProfileToSave.SetRoles(mRoles);
                                        }
                                    }
                                    mAccountProfileToSave.AddedBy   = mCurrentAccountProfile.Id;
                                    mAccountProfileToSave.AddedDate = DateTime.Now;
                                    AccountUtility.Save(mAccountProfileToSave, mSaveRoles, mSaveGroups);
                                    mLog.Debug("Saved account " + mAccountProfileToSave.Account + " by " + mCurrentAccountProfile.Account);
                                    mRetVal = "true";
                                }
                                else
                                {
                                    Exception mError = new Exception("The account (" + AccountUtility.CurrentProfile().Account + ") being used does not have the correct permissions to edit");
                                    mLog.Error(mError);
                                    return(this.InternalServerError(mError));
                                }
                            }
                            else
                            {
                                if (mSecurityInfo.MayAdd)
                                {
                                    mSaveGroups                     = true;
                                    mSaveRoles                      = true;
                                    mAccountProfileToSave           = populateAccountProfile(uiProfile, mAccountProfileToSave);
                                    mAccountProfileToSave.Id        = -1;
                                    mAccountProfileToSave.AddedBy   = mCurrentAccountProfile.Id;
                                    mAccountProfileToSave.AddedDate = DateTime.Now;

                                    mAccountProfileToSave.AddedBy         = mCurrentAccountProfile.Id;
                                    mAccountProfileToSave.AddedDate       = DateTime.Now;
                                    mAccountProfileToSave.PasswordLastSet = DateTime.Now;
                                    mAccountProfileToSave.LastLogOn       = DateTime.Now;
                                    mAccountProfileToSave.Password        = CryptoUtility.Encrypt(ConfigSettings.RegistrationPassword, ConfigSettings.EncryptionType);
                                    mAccountProfileToSave.Status          = ConfigSettings.AutoCreateAccountStatusId;
                                    string mGroups = string.Join(",", uiProfile.AccountGroups.Groups);
                                    string mRoles  = string.Join(",", uiProfile.AccountRoles.Roles);
                                    mAccountProfileToSave.SetGroups(mGroups);
                                    mAccountProfileToSave.SetRoles(mRoles);
                                    try
                                    {
                                        AccountUtility.Save(mAccountProfileToSave, mSaveRoles, mSaveGroups);
                                    }
                                    catch (Exception ex)
                                    {
                                        mLog.Error(ex);
                                    }
                                    mLog.Debug("Added account " + mAccountProfileToSave.Account + " by " + mCurrentAccountProfile.Account);
                                    mRetVal = "true";
                                }
                                else
                                {
                                    Exception mError = new Exception("The account (" + AccountUtility.CurrentProfile().Account + ") being used does not have the correct permissions to add");
                                    mLog.Error(mError);
                                    return(this.InternalServerError(mError));
                                }
                            }
                        }
                        else
                        {
                            Exception mError = new Exception("Security Info is not in context nothing has been saved!!!!");
                            mLog.Error(mError);
                            return(this.InternalServerError(mError));
                        }
                    }
                    else
                    {
                        Exception mError = new Exception("Identifier you have last looked at does not match the one passed in nothing has been saved!!!!");
                        mLog.Error(mError);
                        return(this.InternalServerError(mError));
                    }
                }
            }

            return(Ok(mRetVal));
        }