예제 #1
0
    public void AddRemoveUsers_NullArguments()
    {
        PolicyDecision policy = new PolicyDecision(config);

        Assert.Throws(typeof(ArgumentNullException),
          () => policy.AddUsersToRoles(null, new String[] { }));
        Assert.Throws(typeof(ArgumentNullException),
          () => policy.AddUsersToRoles(new String[] { }, null));
        Assert.Throws(typeof(ArgumentNullException),
          () => policy.RemoveUsersFromRoles(null, new String[] { }));
        Assert.Throws(typeof(ArgumentNullException),
          () => policy.RemoveUsersFromRoles(new String[] { }, null));
    }
예제 #2
0
    public void CreateRole_AlreadyExists()
    {
        String role = "Some role";

        PolicyDecision policy = new PolicyDecision(config);
        policy.ApplicationName = "Application 1";
        if (policy.RoleExists(role))
          policy.DeleteRole(role, false);

        Assert.IsFalse(policy.RoleExists(role));
        policy.CreateRole("Some role");

        Assert.IsTrue(policy.RoleExists(role));
        policy.CreateRole("Some role");
    }
예제 #3
0
파일: Main.cs 프로젝트: davidajulio/hx
    static void Main()
    {
        PolicyDecision policy = new PolicyDecision();
        policy.ApplicationName = "Application 1";

        String roleName = "Another: " + DateTime.Now;
        policy.CreateRole(roleName);

        Console.WriteLine("AllRoles: " + String.Join(", ", policy.GetAllRoles()));

        Console.WriteLine("Users from Role 1: " + String.Join(", ", policy.GetUsersInRole("Role 1")));

        Console.WriteLine("Is User 1 in Role 1? " + policy.IsUserInRole("User 1", "Role 1"));
        Console.WriteLine("Is User 3 in Role 1? " + policy.IsUserInRole("User 3", "Role 1"));

        policy.DeleteRole(roleName, true);
        policy.CreateRole("Test: " + DateTime.Now);
    }
예제 #4
0
    public void Demand()
    {
        PolicyDecision decision = new PolicyDecision(config);
        decision.ApplicationName = "Application 3";

        PolicyPermission permission = new PolicyPermission("Permission 1", decision);

        IIdentity identity = new GenericIdentity("User 1");
        IPrincipal principal = new GenericPrincipal(identity, new String[] { "Role 1" });
        Thread.CurrentPrincipal = principal;
        Assert.Throws(typeof(SecurityException),
          permission.Demand);

        identity = new GenericIdentity("User 2");
        principal = new GenericPrincipal(identity, new String[] { "Role 1", "Role 2", "Role 3" });
        Thread.CurrentPrincipal = principal;
        Assert.DoesNotThrow(permission.Demand);
    }
예제 #5
0
    public void AddRemoveUsers_UserAlreadyExists()
    {
        PolicyDecision policy = new PolicyDecision(config);
        policy.ApplicationName = "Application 2";

        if (false == policy.RoleExists("Role 1"))
          policy.CreateRole("Role 1");

        if (policy.IsUserInRole("User 1", "Role 1"))
          policy.RemoveUsersFromRoles(new String[] { "User 1" },
                  new String[] { "Role 1" });

        Assert.DoesNotThrow(() => policy.AddUsersToRoles(new String[] { "User 1" },
                             new String[] { "Role 1" }));

        Assert.Throws(typeof(ProviderException),
          () => policy.AddUsersToRoles(new String[] { "User 1" },
                           new String[] { "Role 1" }));

        policy.RemoveUsersFromRoles(new String[] { "User 1" },
                new String[] { "Role 1" });
        policy.DeleteRole("Role 1", false);
    }
예제 #6
0
    public void Role_GetHierarchy()
    {
        PolicyDecision policy = new PolicyDecision(config);
        policy.ApplicationName = "Application 1";

        String[] roles = {"Test Role", "Test Role 2", "Test Role 3"};

        for (Int32 i = 0; i != roles.Length; ++i) {
          if (policy.RoleExists(roles[i]))
        continue;
          if (0 == i)
        policy.CreateRole(roles[i]);
          else
        policy.CreateRole(roles[i], roles[i-1]);
        }

        policy.AddUsersToRoles(new String[] { "User" },
               new String[] { roles[2] });

        String[] userRoles = policy.GetRolesForUser("User");
        Assert.AreEqual(3, userRoles.Length);
        foreach (String role in roles)
          Assert.IsTrue(Array.Exists(userRoles, v => v == role));

        for (Int32 i = roles.Length - 1; i >= 0; --i)
          policy.DeleteRole(roles[i], false);
    }
예제 #7
0
 public void Application_InvalidApplication()
 {
     PolicyDecision policy = new PolicyDecision(config);
     policy.ApplicationName = "Doesn't exist";
     policy.CreateRole("Some role");
 }
예제 #8
0
    public void UserRole_InvalidNames()
    {
        PolicyDecision policy = new PolicyDecision(config);

        Type[] expectations = new Type[] {
          typeof(ProviderException),
          typeof(ProviderException),
          typeof(ArgumentException)
        };
        String[] values = new String[] {
          String.Empty,
          null,
          "something , another ,"
        };
        Action<String>[] tests = new Action<String>[] {
          (value) => policy.CreateRole(value),
        (value) => policy.DeleteRole(value, false),
        (value) => policy.RoleExists(value),
        (value) => policy.GetUsersInRole(value),
        (value) => policy.IsUserInRole("Valid", value),
        (value) => policy.IsUserInRole(value, "Valid"),
        (value) => policy.GetRolesForUser(value),
        (value) => policy.FindUsersInRole(value, "Valid"),
        (value) => policy.FindUsersInRole("Valid", value)
        };

        for (Int32 i = 0; i != tests.Length; ++i)
          for (Int32 j = 0; j != values.Length; ++j)
        Assert.Throws(expectations[j], () => tests[i](values[j]));
    }
예제 #9
0
 internal PolicyPermission(String permission, PolicyDecision decision)
     : this(new List<String>() { permission })
 {
     this.decision = decision;
 }
예제 #10
0
 public PolicyPermission(String permission)
     : this(new List<String>() { permission })
 {
     this.decision = new PolicyDecision();
 }