/// <summary>
        /// Builds the DbCommand to delete a dependency record for the given taskQueueItem from the
        /// TaskDependencies table.
        /// </summary>
        /// <param name="daMgr">DataAccessMgr object</param>
        /// <param name="taskQueueItem">Unique Identifier of the TaskDependencies record to delete</param>
        /// <returns>DbCommand Object with given Parameter values</returns>
        public static DbCommand GetDeleteDependencyTaskCmd(DataAccessMgr daMgr, DataRow taskQueueItem)
        {
            if (taskQueueItem == null ||
                !taskQueueItem.Table.Columns.Contains(TaskProcessing.Constants.TaskQueueCode))
            {
                throw new ExceptionEvent(enumExceptionEventCodes.NullOrEmptyParameter
                                         , "DataRow (taskQueueItem) containing TaskProcessingQueue data was empty");
            }

            DbTableDmlMgr dmlMgr = daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                 , TaskProcessing.Constants.TaskDependencies);

            dmlMgr.SetWhereCondition(w => w.Column(TaskProcessing.Constants.TaskQueueCode)
                                     == w.Parameter(TaskProcessing.Constants.TaskQueueCode));
            DbCommand dbCmd = daMgr.BuildDeleteDbCommand(dmlMgr);

            dbCmd.Parameters[daMgr.BuildParamName(TaskProcessing.Constants.TaskQueueCode)].Value
                = Convert.ToInt32(taskQueueItem[TaskProcessing.Constants.TaskQueueCode]);
            return(dbCmd);
        }
        /// <summary>
        /// Sets or resets an account restriction for the given user identifier
        /// </summary>
        /// <param name="daMgr">DataAccessMgr object</param>
        /// <param name="userId">Unique user identifier</param>
        /// <param name="removeRestriction">Indicates whether to set or reset the restriction</param>
        static void Restriction(DataAccessMgr daMgr, string userId, bool removeRestriction)
        {
            DbTableDmlMgr dmlUpdate = daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                    , Constants.UserMaster);

            dmlUpdate.AddColumn(Constants.SignonRestricted
                                , daMgr.BuildParamName(Constants.SignonRestricted));

            dmlUpdate.SetWhereCondition((j) => j.Column(Constants.UserId)
                                        == j.Parameter(dmlUpdate.MainTable.SchemaName
                                                       , dmlUpdate.MainTable.TableName
                                                       , Constants.UserId
                                                       , daMgr.BuildParamName(Constants.UserId)));

            DbCommand cmdUpdate = daMgr.BuildUpdateDbCommand(dmlUpdate);

            cmdUpdate.Parameters[daMgr.BuildParamName(Constants.UserId)].Value
                = userId;
            cmdUpdate.Parameters[daMgr.BuildParamName(Constants.SignonRestricted)].Value
                = removeRestriction ? 0 : 1;
            daMgr.ExecuteNonQuery(cmdUpdate, null, null);
        }
Beispiel #3
0
        DbCommand GetDeleteSessionRecordCmd()
        {
            DbTableStructure tblAppSesssion = _daMgr.DbCatalogGetTable(DataAccess.Constants.SCHEMA_CORE
                                                                       , Constants.SignonControl);
            DbTableDmlMgr dmlDeleteMgr = _daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                        , Constants.AppSessions
                                                                        , Constants.AppId
                                                                        , Constants.StartDateTime);

            dmlDeleteMgr.SetWhereCondition((j) => j.Column(Constants.AppCode)
                                           == j.Parameter(dmlDeleteMgr.MainTable.SchemaName
                                                          , dmlDeleteMgr.MainTable.TableName
                                                          , Constants.AppCode
                                                          , _daMgr.BuildParamName(Constants.AppCode)) &&
                                           j.Column(Constants.MultipleSessionCode)
                                           == j.Parameter(dmlDeleteMgr.MainTable.SchemaName
                                                          , dmlDeleteMgr.MainTable.TableName
                                                          , Constants.MultipleSessionCode
                                                          , _daMgr.BuildParamName(Constants.MultipleSessionCode)));

            return(_daMgr.BuildDeleteDbCommand(dmlDeleteMgr));
        }
        /// <summary>
        /// Implements the user signon operation
        /// </summary>
        /// <param name="daMgr">DataAccessMgr object</param>
        /// <param name="signonControl">SignonControl data structure</param>
        /// <param name="userId">Unique user identifier</param>
        /// <param name="userPassword">User's password (NULL for first time initialization)</param>
        /// <param name="userEnv">Meta data about user's environment</param>
        /// <param name="allowMultipleSessions">Indicates whether to allow multiple session for an account that was not setup for multiple sessions</param>
        /// <returns>SignonResult data structure</returns>
        public static SignonResultsStructure Signon(DataAccessMgr daMgr
                                                    , SignonControl signonControl
                                                    , string userId
                                                    , string userPassword
                                                    , UserEnvironmentStructure userEnv
                                                    , bool allowMultipleSessions = false)
        {
            SignonResultsStructure results = new SignonResultsStructure();

            results.ResultEnum    = SignonResultsEnum.Success;
            results.ResultMessage = null;

            DbTableDmlMgr dmlSelectMgr = daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                       , Constants.UserMaster
                                                                       , Constants.UserCode
                                                                       , Constants.UserPassword
                                                                       , Constants.PasswordSalt
                                                                       , Constants.SignonRestricted
                                                                       , Constants.LastSignonDateTime
                                                                       , Constants.FailedSignonAttempts
                                                                       , Constants.ForcePasswordChange
                                                                       , Constants.MultipleSignonAllowed
                                                                       , Constants.DefaultAccessGroupCode);

            dmlSelectMgr.SetWhereCondition((j) => j.Column(Constants.UserId)
                                           == j.Parameter(dmlSelectMgr.MainTable.SchemaName
                                                          , dmlSelectMgr.MainTable.TableName
                                                          , Constants.UserId
                                                          , daMgr.BuildParamName(Constants.UserId)));
            DbCommand cmdSelectUserMaster = daMgr.BuildSelectDbCommand(dmlSelectMgr, null);

            cmdSelectUserMaster.Parameters[daMgr.BuildParamName(Constants.UserId)].Value = userId;

            DbCommandMgr dbCmdMgr = new DbCommandMgr(daMgr);

            dbCmdMgr.AddDbCommand(cmdSelectUserMaster);
            if (!allowMultipleSessions)
            {
                dmlSelectMgr = daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                             , Constants.UserSessions
                                                             , Constants.SessionCode
                                                             , Constants.SessionDateTime
                                                             , Constants.ForceSignOff);
                dmlSelectMgr.SetWhereCondition((j) => j.Column(Constants.UserId)
                                               == j.Parameter(dmlSelectMgr.MainTable.SchemaName
                                                              , dmlSelectMgr.MainTable.TableName
                                                              , Constants.UserId
                                                              , daMgr.BuildParamName(Constants.UserId)));
                DbCommand cmdSelectSessions = daMgr.BuildSelectDbCommand(dmlSelectMgr, null);
                cmdSelectSessions.Parameters[daMgr.BuildParamName(Constants.UserId)].Value = userId;
                dbCmdMgr.AddDbCommand(cmdSelectSessions);
            }

            List <string> tableNames = new List <string>();

            tableNames.Add(Constants.UserMaster);

            if (!allowMultipleSessions)
            {
                tableNames.Add(Constants.UserSessions);
            }

            DataSet userSigonData = dbCmdMgr.ExecuteDataSet(tableNames);

            DataTable userMaster = userSigonData.Tables[Constants.UserMaster];

            // see if the userId exists and that the password is correct
            if (userMaster.Rows.Count == 0)
            {
                // userId does not exists, return an invalid credentials message
                results.ResultEnum    = SignonResultsEnum.InvaldCredentials;
                results.ResultMessage = "Incorrect UserId or Password, please try again.";
                return(results);
            }

            string storedUserPassword = userMaster.Rows[0][Constants.UserPassword].ToString();
            string passwordSalt       = userMaster.Rows[0][Constants.PasswordSalt].ToString();

            userPassword = Cryptography.HashOperation.ComputeHash(HashAlgorithmTypeEnum.SHA512HashAlgorithm, userPassword, passwordSalt);
            if (storedUserPassword != userPassword)
            {
                // invalid credentials;  do not indicate whether userId or password is incorrect
                results.ResultEnum    = SignonResultsEnum.InvaldCredentials;
                results.ResultMessage = "Incorrect UserId or Password, please try again.";
                Int16 failedAttempts = IncreaseFailedAttemptCount(daMgr, userId);
                // check for failed limit and restrict account
                if (failedAttempts >= signonControl.SignonControlData.FailedAttemptLimit)
                {
                    RestrictSignon(daMgr, userId);
                }
                return(results);
            }

            // Since the UserId and Password matched, we found the account,
            // now check for account level restrictions
            bool signonRestricted = Convert.ToBoolean(userMaster.Rows[0][Constants.SignonRestricted]);

            if (signonRestricted)
            {
                // invalid credentials;  do not indicate whether userId or password is incorrect
                results.ResultEnum    = SignonResultsEnum.SignonsRestricted;
                results.ResultMessage = "The account is restrcited from signing on.";
                return(results);
            }

            bool ForcePasswordChange = Convert.ToBoolean(userMaster.Rows[0][Constants.ForcePasswordChange]);

            if (ForcePasswordChange)
            {
                // invalid credentials;  do not indicate whether userId or password is incorrect
                results.ResultEnum    = SignonResultsEnum.PasswordChangeRequired;
                results.ResultMessage = "The account requires a password change before proceeding.";
                return(results);
            }

            if (!allowMultipleSessions)
            {
                bool      MultipleSignonAllowed = Convert.ToBoolean(userMaster.Rows[0][Constants.MultipleSignonAllowed]);
                DataTable userSessions          = userSigonData.Tables[Constants.UserSessions];
                Int16     sessionCount          = 0;
                foreach (DataRow userSession in userSessions.Rows)
                {
                    DateTime sessionDateTime = Convert.ToDateTime(userSession[Constants.SessionDateTime]);
                    TimeSpan sessionInterval = daMgr.DbSynchTime - sessionDateTime;
                    if (sessionInterval.TotalSeconds < signonControl.SignonControlData.TimeOutSeconds)
                    {
                        if (!MultipleSignonAllowed)
                        {
                            // if the user cannot have multiple signons, then we must check
                            // for existing (Active) session
                            results.ResultEnum    = SignonResultsEnum.MultipleSignonRestricted;
                            results.ResultMessage = "The account can only have a single signon session.  They must signOff the other session first.";
                            return(results);
                        }
                        ++sessionCount;
                    }
                }
            }

            // if the userId and password are correct, check signon control (general restrictions)
            if (signonControl.SignonControlData.RestrictSignon)
            {
                results.ResultEnum    = SignonResultsEnum.SignonsRestricted;
                results.ResultMessage = signonControl.SignonControlData.RestrictSignonMsg;
                return(results);
            }

            if (signonControl.SignonControlData.ForceSignoff)
            {
                results.ResultEnum    = SignonResultsEnum.ForcedSignoff;
                results.ResultMessage = signonControl.SignonControlData.SignoffWarningMsg;
                return(results);
            }

            // successful signon
            UserSignonSessionStructure uss = new UserSignonSessionStructure();

            uss.UserCode               = Convert.ToInt32(userMaster.Rows[0][Constants.UserCode]);
            uss.PasswordHash           = userMaster.Rows[0][Constants.UserPassword].ToString();
            uss.DefaultAccessGroupCode = Convert.ToInt32(userMaster.Rows[0][Constants.DefaultAccessGroupCode]);
            uss.UserId               = userId;
            uss.SignonApp.AppCode    = userEnv.AppCode;
            uss.SignonApp.AppId      = userEnv.AppId;
            uss.SignonApp.AppVersion = userEnv.AppVersion;
            uss.SessionCode          = AddSession(daMgr, userId, uss.UserCode, userEnv);
            UserSession sessionMgr = new UserSession(daMgr, uss);

            results.ResultMessage = "Welcome.";
            if (userMaster.Rows[0][Constants.LastSignonDateTime] != DBNull.Value)
            {
                DateTime signonDateTime = Convert.ToDateTime(userMaster.Rows[0][Constants.LastSignonDateTime]);
                results.ResultMessage += "  Your last signon was: ." + signonDateTime.ToString();
            }
            results.ResultEnum     = SignonResultsEnum.Success;
            results.UserSessionMgr = sessionMgr;
            return(results);
        }
        /// <summary>
        /// Method with change the settings of the SignonControl table to the given new settings only if they have not already been changed.
        /// <para>If </para>
        /// </summary>
        /// <param name="lastModifiedUser">The userCode of the user who last updated the record (or null if not changed)</param>
        /// <param name="lastModifiedDateTime">The DateTime of when the user last updated the record (or null if not changed)</param>
        /// <param name="newControlSettings">Signon Control Data Strcutre containing the new values.</param>
        /// <returns>Boolean indicating if record was changed or not.</returns>
        internal bool ChangeControl(int?lastModifiedByUserCode
                                    , DateTime?lastModifiedDateTime
                                    , SignonControlStructure newControlSettings)
        {
            DbTableDmlMgr dmlChange = _daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                     , Constants.SignonControl);

            dmlChange.AddColumn(Constants.TimeoutSeconds);
            dmlChange.AddColumn(Constants.SignoffWarningMsg);
            dmlChange.AddColumn(Constants.ForceSignOff);
            dmlChange.AddColumn(Constants.RestrictSignon);
            dmlChange.AddColumn(Constants.StatusSeconds);
            dmlChange.AddColumn(Constants.RestrictSignonMsg);
            dmlChange.AddColumn(Constants.FailedAttemptLimit);

            dmlChange.SetWhereCondition((j) =>
                                        j.Column(Constants.SignonControl) ==
                                        j.Parameter(Constants.SignonControl));

            DbCommandMgr dbCmdMgr      = new DbCommandMgr(_daMgr);
            DbCommand    cmdChangeOrig = _daMgr.BuildChangeDbCommand(dmlChange
                                                                     , Constants.LastModifiedUserCode
                                                                     , Constants.LastModifiedDateTime);

            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.ControlCode)].Value
                = newControlSettings.SessionControlCode;
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.TimeoutSeconds)].Value
                = Convert.ToInt16(newControlSettings.TimeOutSeconds);
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.SignoffWarningMsg)].Value
                = newControlSettings.SignoffWarningMsg;
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.ForceSignOff)].Value
                = newControlSettings.ForceSignoff;
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.RestrictSignon)].Value
                = newControlSettings.RestrictSignon;
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.RestrictSignonMsg)].Value
                = newControlSettings.RestrictSignonMsg;
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.StatusSeconds)].Value
                = newControlSettings.StatusSeconds;
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.FailedAttemptLimit)].Value
                = newControlSettings.FailedAttemptLimit;
            if (lastModifiedByUserCode.HasValue)
            {
                cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.LastModifiedUserCode)].Value
                    = lastModifiedByUserCode.Value;
            }
            if (lastModifiedDateTime.HasValue)
            {
                cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.LastModifiedDateTime)].Value
                    = lastModifiedDateTime.Value;
            }
            if (newControlSettings.LastModifiedByUserCode.HasValue)
            {
                cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.LastModifiedUserCode, true)].Value
                    = newControlSettings.LastModifiedByUserCode.Value;
            }
            cmdChangeOrig.Parameters[_daMgr.BuildParamName(Constants.LastModifiedDateTime, true)].Value
                = newControlSettings.LastModifiedByDateTime.Value;

            int rowsAffected = _daMgr.ExecuteNonQuery(cmdChangeOrig, null, null);

            if (rowsAffected == 1)
            {
                _signonControl.FailedAttemptLimit     = newControlSettings.FailedAttemptLimit;
                _signonControl.ForceSignoff           = newControlSettings.ForceSignoff;
                _signonControl.RestrictSignon         = newControlSettings.RestrictSignon;
                _signonControl.RestrictSignonMsg      = newControlSettings.RestrictSignonMsg;
                _signonControl.SignoffWarningMsg      = newControlSettings.SignoffWarningMsg;
                _signonControl.StatusSeconds          = newControlSettings.StatusSeconds;
                _signonControl.TimeOutSeconds         = newControlSettings.TimeOutSeconds;
                _signonControl.LastModifiedByUserCode = newControlSettings.LastModifiedByUserCode;
                _signonControl.LastModifiedByDateTime = newControlSettings.LastModifiedByDateTime;

                return(true);
            }
            else
            {
                return(false);
            }
        }