Beispiel #1
0
        /// <summary>
        /// Throws exception if 'param' is Null or Empty or if it contains any values that are empty, contain commas, or are too big
        /// </summary>
        /// <param name="param">The array of strings to validate</param>
        /// <param name="checkForNull">If true, validates that param values are not null</param>
        /// <param name="checkIfEmpty">If true, validates that param values are not blank</param>
        /// <param name="checkForCommas">If true, validates that param values do not contain one or more commas</param>
        /// <param name="maxSize">Specify '0' if maxSize should not be checked</param>
        /// <param name="paramName">The name of the parameter to validate</param>
        internal static void CheckArrayParameter(ref string[] param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize, string paramName)
        {
            if (param == null)
            {
                throw new ArgumentNullException(paramName);
            }

            if (param.Length < 1)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, StringResources.Parameter_array_empty, paramName), paramName);
            }

            //Create a hashtable to help determine if there are duplicate values in the array of strings
            Hashtable values = new Hashtable(param.Length);

            for (int i = param.Length - 1; i >= 0; i--)
            {
                SecurityUtility.CheckParameter(ref param[i], checkForNull, checkIfEmpty, checkForCommas, maxSize, paramName + "[ " + i.ToString(CultureInfo.InvariantCulture) + " ]");
                if (values.Contains(param[i]))
                {
                    throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, StringResources.Parameter_duplicate_array_element, paramName), paramName);
                }
                else
                {
                    values.Add(param[i], param[i]);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns the appName and commandTimeout values found in the NameValueCollection
        /// </summary>
        /// <param name="config"></param>
        /// <param name="appName"></param>
        /// <param name="commandTimeout"></param>
        internal static void InitializeCommonParameters(NameValueCollection config, out string appName, out int commandTimeout)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            //Make a copy of the NameValueCollection so that we do not accidentally change any referenced values
            NameValueCollection values = new NameValueCollection(config);

            commandTimeout = SecurityUtility.GetIntValue(values, "commandTimeout", 30, true, 0);

            appName = values["applicationName"];
            if (string.IsNullOrEmpty(appName))
            {
                appName = SecurityUtility.DefaultAppName;
            }

            if (appName.Length > 256)
            {
                throw new ProviderException(StringResources.Provider_application_name_too_long);
            }

            values.Remove("applicationName");
            values.Remove("commandTimeout");
            if (values.Count > 0)
            {
                string attribUnrecognized = values.GetKey(0);
                if (!String.IsNullOrEmpty(attribUnrecognized))
                {
                    throw new ProviderException(String.Format(CultureInfo.CurrentCulture, StringResources.Provider_unrecognized_attribute, attribUnrecognized));
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets an array of user names in a role where the user name contains the specified user name to match.
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="usernameToMatch"></param>
        /// <returns></returns>
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            SecurityUtility.CheckParameter(ref roleName, true, true, true, StringResources.Name_Max_Size, "roleName");
            SecurityUtility.CheckParameter(ref usernameToMatch, true, true, false, StringResources.Name_Max_Size, "usernameToMatch");

            //TODO: Implement the Contains cop-out isn't very robust (should allow wildcards)
            //Also, the user list that is returned is supposed to be sorted alphabetically
            Role role = _roleRepository.GetByRoleName(roleName);

            if (!role.AppUser.IsLoaded)
            {
                role.AppUser.Load();
            }

            StringCollection userList = new StringCollection();

            foreach (AppUser user in role.AppUser)
            {
                if (user.UserName.ToUpperInvariant().Contains(usernameToMatch.ToUpperInvariant()))
                {
                    userList.Add(user.UserName);
                }
            }

            String[] usersForRole = new String[userList.Count];
            userList.CopyTo(usersForRole, 0);
            return(usersForRole);
        }
Beispiel #4
0
        /// <summary>
        /// Gets a list of the roles that a specified user is in.
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public override string[] GetRolesForUser(string username)
        {
            SecurityUtility.CheckParameter(ref username, true, false, true, StringResources.Name_Max_Size, "username");
            if (username.Length < 1)
            {
                return(new string[0]);
            }

            AppUser user = _userRepository.GetByUserName(username);

            if (!user.Role.IsLoaded)
            {
                user.Role.Load();
            }

            StringCollection roleList = new StringCollection();

            foreach (Role role in user.Role)
            {
                roleList.Add(role.RoleName);
            }

            String[] rolesForUser = new String[roleList.Count];
            roleList.CopyTo(rolesForUser, 0);
            return(rolesForUser);
        }
Beispiel #5
0
        /// <summary>
        /// Gets a value indicating whether the specified role name already exists in the role data source
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override bool RoleExists(string roleName)
        {
            SecurityUtility.CheckParameter(ref roleName, true, true, true, StringResources.Name_Max_Size, "roleName");

            List <Role> results = _roleRepository.FindByRoleName(roleName);

            return(0 < results.Count);
        }
Beispiel #6
0
        /// <summary>
        /// Gets a value indicating whether the specified user is in the specified role.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override bool IsUserInRole(string username, string roleName)
        {
            SecurityUtility.CheckParameter(ref roleName, true, true, true, StringResources.Name_Max_Size, "roleName");
            SecurityUtility.CheckParameter(ref username, true, false, true, StringResources.Name_Max_Size, "username");

            if (username.Length < 1 || roleName.Length < 1)
            {
                return(false);
            }

            return(_roleRepository.IsUserInRole(username, roleName));
        }
Beispiel #7
0
        /// <summary>
        /// Initializes the Entity Framework role provider with the property values specified in the application's configuration file
        /// </summary>
        /// <param name="name"></param>
        /// <param name="config"></param>
        public override void Initialize(string name, NameValueCollection config)
        {
            //TraceUtility.LogMethodEntry();

            if (String.IsNullOrEmpty(name))
            {
                name = "L2ERoleProvider";
            }

            base.Initialize(name, config);

            SecurityUtility.InitializeCommonParameters(config, out _appName, out _commandTimeout);
        }
Beispiel #8
0
        /// <summary>
        /// Adds a new role to the data source
        /// </summary>
        /// <param name="roleName"></param>
        public override void CreateRole(string roleName)
        {
            SecurityUtility.CheckParameter(ref roleName, true, true, true, StringResources.Name_Max_Size, "roleName");

            if (RoleExists(roleName))
            {
                throw new ProviderException(String.Format(CultureInfo.CurrentCulture, StringResources.Provider_role_already_exists, roleName));
            }

            Role roleDetail = _roleRepository.New();

            roleDetail.RoleName = roleName;

            _roleRepository.Save(roleDetail);
        }
Beispiel #9
0
        /// <summary>
        /// Removes the specified user names from the specified roles
        /// </summary>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            SecurityUtility.CheckArrayParameter(ref roleNames, true, true, true, StringResources.Name_Max_Size, "roleNames");
            SecurityUtility.CheckArrayParameter(ref usernames, true, true, true, StringResources.Name_Max_Size, "usernames");

            Collection <AppUser> users = new Collection <AppUser>();
            Collection <Role>    roles = new Collection <Role>();

            foreach (string username in usernames)
            {
                users.Add(_userRepository.GetByUserName(username));
                foreach (string rolename in roleNames)
                {
                    roles.Add(_roleRepository.GetByRoleName(rolename));
                }
            }
            RemoveUsersFromRoles(users, roles);
        }
Beispiel #10
0
        /// <summary>
        /// Removes a role from the data source
        /// </summary>
        /// <param name="roleName"></param>
        /// <param name="throwOnPopulatedRole"></param>
        /// <returns></returns>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            SecurityUtility.CheckParameter(ref roleName, true, true, true, StringResources.Name_Max_Size, "roleName");

            Role roleToDelete = _roleRepository.GetByRoleName(roleName);

            if (!roleToDelete.AppUser.IsLoaded)
            {
                roleToDelete.AppUser.Load();
            }

            if (throwOnPopulatedRole && 0 < roleToDelete.AppUser.Count)
            {
                //We do not want to delete all these users that are part of this role
                throw new ProviderException(StringResources.Role_is_not_empty);
            }

            _roleRepository.Delete(roleToDelete);
            return(true);
        }
Beispiel #11
0
        /// <summary>
        /// Gets a list of users in the specified role
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public override string[] GetUsersInRole(string roleName)
        {
            SecurityUtility.CheckParameter(ref roleName, true, true, true, 256, "roleName");

            Role role = _roleRepository.GetByRoleName(roleName);

            if (!role.AppUser.IsLoaded)
            {
                role.AppUser.Load();
            }

            StringCollection userList = new StringCollection();

            foreach (AppUser user in role.AppUser)
            {
                userList.Add(user.UserName);
            }

            String[] usersForRole = new String[userList.Count];
            userList.CopyTo(usersForRole, 0);
            return(usersForRole);
        }