/// <summary>
        /// Adds a record to the session database table
        /// </summary>
        /// <param name="daMgr">DataAccessMgr object</param>
        /// <param name="userId">Unique user identifier</param>
        /// <param name="userCode">Unique numeric user identifier</param>
        /// <param name="userEnv">MetaData about the user's environment</param>
        /// <returns>A unique session code</returns>
        static Int64 AddSession(DataAccessMgr daMgr, string userId, Int32 userCode, UserEnvironmentStructure userEnv)
        {
            Int64         sessionCode = daMgr.GetNextSequenceNumber(Constants.SessionCode);
            DbTableDmlMgr dmlInsert   = daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                      , Constants.UserSessions);

            dmlInsert.AddColumn(Constants.SessionCode, daMgr.BuildParamName(Constants.SessionCode));
            dmlInsert.AddColumn(Constants.UserCode, daMgr.BuildParamName(Constants.UserCode));
            dmlInsert.AddColumn(Constants.UserId, daMgr.BuildParamName(Constants.UserId));
            dmlInsert.AddColumn(Constants.AppCode, daMgr.BuildParamName(Constants.AppCode));
            dmlInsert.AddColumn(Constants.AppId, daMgr.BuildParamName(Constants.AppId));
            dmlInsert.AddColumn(Constants.AppMachine, daMgr.BuildParamName(Constants.AppMachine));
            dmlInsert.AddColumn(Constants.AppVersion, daMgr.BuildParamName(Constants.AppVersion));
            dmlInsert.AddColumn(Constants.RemoteAddress, daMgr.BuildParamName(Constants.RemoteAddress));

            DbCommand cmdInsert = daMgr.BuildInsertDbCommand(dmlInsert);

            cmdInsert.Parameters[daMgr.BuildParamName(Constants.SessionCode)].Value   = sessionCode;
            cmdInsert.Parameters[daMgr.BuildParamName(Constants.UserId)].Value        = userId;
            cmdInsert.Parameters[daMgr.BuildParamName(Constants.UserCode)].Value      = userCode;
            cmdInsert.Parameters[daMgr.BuildParamName(Constants.AppId)].Value         = userEnv.AppId;
            cmdInsert.Parameters[daMgr.BuildParamName(Constants.AppCode)].Value       = userEnv.AppCode;
            cmdInsert.Parameters[daMgr.BuildParamName(Constants.AppVersion)].Value    = userEnv.AppVersion;
            cmdInsert.Parameters[daMgr.BuildParamName(Constants.RemoteAddress)].Value = userEnv.RemoteAddress;
            cmdInsert.Parameters[daMgr.BuildParamName(Constants.AppMachine)].Value    = Environment.MachineName;

            DbTableDmlMgr dmlUpdate = daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                    , Constants.UserMaster);

            dmlUpdate.AddColumn(Constants.FailedSignonAttempts
                                , daMgr.BuildParamName(Constants.FailedSignonAttempts));
            dmlUpdate.AddColumn(Constants.LastSignonDateTime
                                , EnumDateTimeLocale.Default);

            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.FailedSignonAttempts)].Value
                = 0;
            DbCommandMgr cmdMgr = new DbCommandMgr(daMgr);

            cmdMgr.AddDbCommand(cmdInsert);
            cmdMgr.AddDbCommand(cmdUpdate);
            cmdMgr.ExecuteNonQuery();
            return(sessionCode);
        }
Example #2
0
        /// <summary>
        /// Constructor for a new application session which verifies AppId.
        /// <para>To begin a new session, the Start method must be called.</para>
        /// </summary>
        /// <param name="daMgr">DataAccessMgr object</param>
        /// <param name="appId">Application's unique string identifier</param>
        /// <param name="appVersion">Application's version string</param>
        /// <param name="appName">Applications's name</param>
        /// <param name="heartBeatStatusHdlr">The application's delegate to call during every heartbeat to get the status</param>
        public AppSession(DataAccessMgr daMgr
                          , string appId
                          , string appVersion
                          , string appName
                          , HeartbeatStatusHandler heartBeatStatusHdlr)
        {
            _daMgr               = daMgr;
            _appId               = appId;
            _appName             = appName;
            _appVersion          = appVersion;
            _heartbeatStatusHdlr = heartBeatStatusHdlr;
            _signonControl       = new SignonControl(_daMgr);

            DbTableDmlMgr dmlSelectMgr = _daMgr.DbCatalogGetTableDmlMgr(DataAccess.Constants.SCHEMA_CORE
                                                                        , Constants.AppMaster, Constants.AppCode, Constants.AllowMultipleSessions);

            dmlSelectMgr.SetWhereCondition((j) => j.Column(Constants.AppId)
                                           == j.Parameter(dmlSelectMgr.MainTable.SchemaName
                                                          , dmlSelectMgr.MainTable.TableName
                                                          , Constants.AppId
                                                          , _daMgr.BuildParamName(Constants.AppId)));
            DbCommand cmdSelect = _daMgr.BuildSelectDbCommand(dmlSelectMgr, null);

            cmdSelect.Parameters[_daMgr.BuildParamName(Constants.AppId)].Value = appId;
            DataTable appMaster = _daMgr.ExecuteDataSet(cmdSelect, null, null).Tables[0];

            if (appMaster.Rows.Count == 0)
            {
                throw new ExceptionEvent(enumExceptionEventCodes.AppCodeNotFound
                                         , string.Format("AppId: {0}", appId));
            }

            _appCode = Convert.ToInt32(appMaster.Rows[0][Constants.AppCode]);
            _allowMultipleSessions = Convert.ToBoolean(appMaster.Rows[0][Constants.AllowMultipleSessions]);
            if (!_allowMultipleSessions)
            {
                _appSessionCode = 0;
            }
            else
            {
                _appSessionCode = _daMgr.GetNextSequenceNumber(Constants.MultipleSessionCode);
            }
        }