/// <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]); } } }
/// <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); }
/// <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); }
/// <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); }
/// <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)); }
/// <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); }
/// <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); }
/// <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); }