/// <summary>
        /// Loads a collection of Role objects for the given user
        /// </summary>
        /// <param name="userId">Id of the user to load the roles for</param>
        /// <returns>A collection of Role objects for the given user</returns>
        public static RoleCollection LoadForUser(Int32 userId)
        {
            //CREATE THE DYNAMIC SQL TO LOAD OBJECT
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT " + Role.GetColumnNames("R"));
            selectQuery.Append(" FROM (ac_Roles R INNER JOIN ac_GroupRoles GR ON R.RoleId = GR.RoleId)");
            selectQuery.Append(" INNER JOIN ac_UserGroups UG ON GR.GroupId = UG.GroupId");
            selectQuery.Append(" WHERE UG.UserId = @userId");
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());

            database.AddInParameter(selectCommand, "@userId", System.Data.DbType.Int32, userId);
            //EXECUTE THE COMMAND
            RoleCollection results = new RoleCollection();

            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read())
                {
                    Role role = new Role();
                    Role.LoadDataReader(role, dr);
                    results.Add(role);
                }
                dr.Close();
            }
            return(results);
        }
        private void InternalReload()
        {
            roleIdLookup = new Dictionary <string, int>();
            RoleCollection allRoles = RoleDataSource.LoadAll();

            foreach (Role role in allRoles)
            {
                roleIdLookup[role.LoweredName] = role.RoleId;
            }
        }
        /// <summary>
        /// Gets a list of all the roles for the configured applicationName.
        /// </summary>
        /// <returns>A string array containing the names of all the roles stored in the data source for the configured applicationName.</returns>
        public override string[] GetAllRoles()
        {
            RoleCollection roleCollection = RoleDataSource.LoadAll();

            string[] allRoles = new string[roleCollection.Count];
            for (int index = 0; index < roleCollection.Count; index++)
            {
                allRoles[index] = roleCollection[index].Name;
            }
            return(allRoles);
        }
        public static RoleCollection LoadForGroup(Int32 groupId, int maximumRows, int startRowIndex, string sortExpression)
        {
            //CREATE THE DYNAMIC SQL TO LOAD OBJECT
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT");
            if (maximumRows > 0)
            {
                selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
            }
            selectQuery.Append(" " + Role.GetColumnNames("ac_Roles"));
            selectQuery.Append(" FROM ac_Roles, ac_GroupRoles");
            selectQuery.Append(" WHERE ac_Roles.RoleId = ac_GroupRoles.RoleId");
            selectQuery.Append(" AND ac_GroupRoles.GroupId = @groupId");
            if (!string.IsNullOrEmpty(sortExpression))
            {
                selectQuery.Append(" ORDER BY " + sortExpression);
            }
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());

            database.AddInParameter(selectCommand, "@groupId", System.Data.DbType.Int32, groupId);
            //EXECUTE THE COMMAND
            RoleCollection results   = new RoleCollection();
            int            thisIndex = 0;
            int            rowCount  = 0;

            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
                {
                    if (thisIndex >= startRowIndex)
                    {
                        Role role = new Role();
                        Role.LoadDataReader(role, dr);
                        results.Add(role);
                        rowCount++;
                    }
                    thisIndex++;
                }
                dr.Close();
            }
            return(results);
        }
        public static RoleCollection  LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression)
        {
            //CREATE THE DYNAMIC SQL TO LOAD OBJECT
            StringBuilder selectQuery = new StringBuilder();

            selectQuery.Append("SELECT");
            if (maximumRows > 0)
            {
                selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
            }
            selectQuery.Append(" " + Role.GetColumnNames(string.Empty));
            selectQuery.Append(" FROM ac_Roles");
            string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " WHERE " + sqlCriteria;

            selectQuery.Append(whereClause);
            if (!string.IsNullOrEmpty(sortExpression))
            {
                selectQuery.Append(" ORDER BY " + sortExpression);
            }
            Database  database      = Token.Instance.Database;
            DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());
            //EXECUTE THE COMMAND
            RoleCollection results   = new RoleCollection();
            int            thisIndex = 0;
            int            rowCount  = 0;

            using (IDataReader dr = database.ExecuteReader(selectCommand))
            {
                while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
                {
                    if (thisIndex >= startRowIndex)
                    {
                        Role role = new Role();
                        Role.LoadDataReader(role, dr);
                        results.Add(role);
                        rowCount++;
                    }
                    thisIndex++;
                }
                dr.Close();
            }
            return(results);
        }
        /// <summary>
        /// Gets a list of the roles that a specified user is in for the configured applicationName.
        /// </summary>
        /// <param name="username">The user to return a list of roles for.</param>
        /// <returns>A string array containing the names of all the roles that the specified user is in for the configured applicationName.</returns>
        public override string[] GetRolesForUser(string username)
        {
            string[] userRoles = new string[0];
            User     user      = UserDataSource.LoadForUserName(username);

            if (user != null)
            {
                RoleCollection roles = RoleDataSource.LoadForUser(user.UserId);
                if (roles.Count > 0)
                {
                    userRoles = new string[roles.Count];
                    for (int i = 0; i < roles.Count; i++)
                    {
                        userRoles[i] = roles[i].Name;
                    }
                }
            }
            return(userRoles);
        }