Beispiel #1
0
        public void will_enforce_single_role()
        {
            var singleRolePolicy = new Policy("single", true, new Role("role1"), new Role("role2"));
            var multiRolePolicy  = new Policy("multi", false, new Role("role1"), new Role("role2"));
            var map = new PolicyMap(singleRolePolicy);

            map.AddAdditionalPolicy(multiRolePolicy);
            var user = new UserDetails()
            {
                UserId = Guid.NewGuid(), RoleNames = new[] { "role1", "role2" }
            };

            var userPolicy = map.GetUserPolicy(user); //default single role policy

            Assert.Collection(userPolicy.Roles, r => string.Equals("role1", r));

            user.PolicyName = "single";
            var userPolicy2 = map.GetUserPolicy(user); //explict single role policy

            Assert.Collection(userPolicy2.Roles, r => string.Equals("role1", r));

            user.PolicyName = "multi";
            var userPolicy3 = map.GetUserPolicy(user);//explict multi role policy

            Assert.Collection(userPolicy3.Roles, r => string.Equals("role1", r), r => string.Equals("role2", r));
        }
        /// <summary>
        /// Returns the policy for the specified name, or null if a policy with the name does not exist.
        /// </summary>
        /// <param name="name">The name of the policy to return.</param>
        /// <returns>The policy for the specified name, or null if a policy with the name does not exist.</returns>
        public AuthorizationPolicy GetPolicy(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            return(PolicyMap.ContainsKey(name) ? PolicyMap[name] : null);
        }
Beispiel #3
0
        public PolicyMapTest()
        {
            //test and usage example

            //building a base policy
            var policy = new Policy(_defaultPolicyName,
                                    false,
                                    new Role(_userRoleName,
                                             new Permission(typeof(AddItem)),
                                             new Permission(typeof(DisplayItem))),
                                    new Role(_adminRoleName,
                                             new Permission(typeof(AddItem)),
                                             new Permission(typeof(DisplayItem)),
                                             new Permission(typeof(DeleteItem)),
                                             new Permission(_customPermissionName))
                                    );

            //building an optional additional policy
            _backupRole = new Role(_backupRoleName, new Permission(typeof(ExportItem)));
            var backupAgentPolicy = new Policy(_backupPolicyName, false, _backupRole);

            //creating the base policy map
            _policyMap = new PolicyMap(policy, _applicationName);
            _policyMap.AddAdditionalPolicy(backupAgentPolicy);

            //create usesrs
            // In usage user details and roles will be recieved from the authenication and permissions system
            _systemUser = new UserDetails
            {
                UserId     = Guid.NewGuid(),
                UserName   = "******",
                PolicyName = _defaultPolicyName,
                RoleNames  = new string[] { _userRoleName, _customRoleName }
            };

            _adminUser = new UserDetails
            {
                UserId     = Guid.NewGuid(),
                UserName   = "******",
                PolicyName = _defaultPolicyName,
                RoleNames  = new string[] { _adminRoleName }
            };
            _backupAgent = new UserDetails
            {
                UserId     = Guid.NewGuid(),
                UserName   = "******",
                PolicyName = _backupPolicyName,
                RoleNames  = new string[] { _backupRoleName }
            };
            dispatcher = new PolicyDispatcher(new Dispatcher(nameof(PolicyMapTest)), () => CurrentPolicy);
            dispatcher.Subscribe <AddItem>(this);
            dispatcher.Subscribe <DisplayItem>(this);
            dispatcher.Subscribe <DeleteItem>(this);
            dispatcher.Subscribe <ExportItem>(this);
            dispatcher.Subscribe <OtherCmd>(this);
            dispatcher.Subscribe <OtherMsg>(this);
        }
        public ILinksPolicy GetPolicy <TResource>(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Policy name cannot be null", nameof(name));
            }
            var policyName = $"{name}:{typeof(TResource).FullName}";

            return(PolicyMap.ContainsKey(policyName) ? PolicyMap[policyName] as ILinksPolicy /*<TResource>*/ : null);
        }
        public string GetPolicyFor(Type messageType)
        {
            Guard.AgainstNull(nameof(messageType), messageType);

            if (!PolicyMap.ContainsKey(messageType))
            {
                return(null);
            }

            return(PolicyMap[messageType]);
        }
    /// <summary>
    /// Adds a new rate limiting policy with the given <paramref name="policyName"/>
    /// </summary>
    /// <param name="policyName">The name to be associated with the given <see cref="RateLimiter"/>.</param>
    /// <param name="partitioner">Method called every time an Acquire or WaitAsync call is made to determine what rate limiter to apply to the request.</param>
    public RateLimiterOptions AddPolicy <TPartitionKey>(string policyName, Func <HttpContext, RateLimitPartition <TPartitionKey> > partitioner)
    {
        ArgumentNullException.ThrowIfNull(policyName);
        ArgumentNullException.ThrowIfNull(partitioner);

        if (PolicyMap.ContainsKey(policyName) || UnactivatedPolicyMap.ContainsKey(policyName))
        {
            throw new ArgumentException($"There already exists a policy with the name {policyName}.", nameof(policyName));
        }

        PolicyMap.Add(policyName, new DefaultRateLimiterPolicy(ConvertPartitioner <TPartitionKey>(policyName, partitioner), null));

        return(this);
    }
        /// <summary>
        /// Returns the policy for the specified name, or null if a policy with the name does not exist.
        /// </summary>
        /// <param name="name">The name of the policy to return.</param>
        /// <returns>The policy for the specified name, or null if a policy with the name does not exist.</returns>
        public AuthorizationPolicy?GetPolicy(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (PolicyMap.TryGetValue(name, out var value))
            {
                return(value);
            }

            return(null);
        }
    /// <summary>
    /// Adds a new rate limiting policy with the given policyName.
    /// </summary>
    /// <param name="policyName">The name to be associated with the given <see cref="IRateLimiterPolicy{TPartitionKey}"/>.</param>
    /// <param name="policy">The <see cref="IRateLimiterPolicy{TPartitionKey}"/> to be applied.</param>
    public RateLimiterOptions AddPolicy <TPartitionKey>(string policyName, IRateLimiterPolicy <TPartitionKey> policy)
    {
        ArgumentNullException.ThrowIfNull(policyName);

        if (PolicyMap.ContainsKey(policyName) || UnactivatedPolicyMap.ContainsKey(policyName))
        {
            throw new ArgumentException($"There already exists a policy with the name {policyName}.", nameof(policyName));
        }

        ArgumentNullException.ThrowIfNull(policy);

        PolicyMap.Add(policyName, new DefaultRateLimiterPolicy(ConvertPartitioner <TPartitionKey>(policyName, policy.GetPartition), policy.OnRejected));

        return(this);
    }
        /// <summary>
        /// Returns the policy for the specified name, or null if a policy with the name does not exist.
        /// </summary>
        /// <param name="name">The name of the policy to return.</param>
        /// <returns>The policy for the specified name, or null if a policy with the name does not exist.</returns>
        public AuthorizationPolicy GetPolicy(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            AuthorizationPolicy policy;

            if (PolicyMap.TryGetValue(name, out policy))
            {
                return(policy);
            }

            return(null);
        }
Beispiel #10
0
    /// <summary>
    /// Adds a new rate limiting policy with the given policyName.
    /// </summary>
    /// <param name="policyName">The name to be associated with the given TPolicy.</param>
    public RateLimiterOptions AddPolicy <TPartitionKey, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TPolicy>(string policyName) where TPolicy : IRateLimiterPolicy <TPartitionKey>
    {
        ArgumentNullException.ThrowIfNull(policyName);

        if (PolicyMap.ContainsKey(policyName) || UnactivatedPolicyMap.ContainsKey(policyName))
        {
            throw new ArgumentException($"There already exists a policy with the name {policyName}.", nameof(policyName));
        }

        var policyType = new PolicyTypeState(typeof(TPolicy));
        Func <IServiceProvider, DefaultRateLimiterPolicy> policyFunc = serviceProvider =>
        {
            var instance = (IRateLimiterPolicy <TPartitionKey>)ActivatorUtilities.CreateInstance(serviceProvider, policyType.PolicyType);
            return(new DefaultRateLimiterPolicy(ConvertPartitioner <TPartitionKey>(policyName, instance.GetPartition), instance.OnRejected));
        };

        UnactivatedPolicyMap.Add(policyName, policyFunc);

        return(this);
    }
Beispiel #11
0
 public AuthorizationPolicy GetPolicy([NotNull] string name)
 {
     return(PolicyMap.ContainsKey(name) ? PolicyMap[name] : null);
 }
Beispiel #12
0
 /// <summary>
 /// Gets the policy based on the <paramref name="name"/>
 /// </summary>
 /// <param name="name">The name of the policy to lookup.</param>
 /// <returns>The <see cref="CorsPolicy"/> if the policy was added.<c>null</c> otherwise.</returns>
 public CorsPolicy GetPolicy([NotNull] string name)
 {
     return(PolicyMap.ContainsKey(name) ? PolicyMap[name] : null);
 }
 internal bool HasDefaultPocily()
 {
     return(PolicyMap.ContainsKey(_defaultPolicyName));
 }
Beispiel #14
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            #region Identity

            modelBuilder.Entity <UserEntity>().ToTable("idn_User");
            modelBuilder.Entity <RoleEntity>().ToTable("idn_Role");
            modelBuilder.Entity <IdentityUserClaim <Guid> >().ToTable("idn_UserClaim");
            modelBuilder.Entity <IdentityUserRole <Guid> >().ToTable("idn_UserRole");
            modelBuilder.Entity <IdentityUserLogin <Guid> >().ToTable("idn_UserLogin");
            modelBuilder.Entity <IdentityRoleClaim <Guid> >().ToTable("idn_RoleClaim");
            modelBuilder.Entity <IdentityUserToken <Guid> >().ToTable("idn_UserToken");

            #endregion

            #region Directory

            modelBuilder.Entity <ApplicationEntity>().ToTable("dir_Application").HasData(SeedData.GetApplications());
            modelBuilder.Entity <OrganisationEntity>().ToTable("dir_Organisation");
            modelBuilder.Entity <UseCaseEntity>().ToTable("dir_UseCase").HasData(SeedData.GetUseCases());
            modelBuilder.Entity <RoleToUseCaseEntity>().ToTable("dir_RoleToUseCase");
            modelBuilder.Entity <BranchEntity>().ToTable("dir_Branch");
            modelBuilder.Entity <AuditLogEntity>().ToTable("dir_AuditLog");
            modelBuilder.Entity <CompanyEntity>().ToTable("dir_Company");
            modelBuilder.Entity <ChangeLogEntity>().ToTable("dir_ChangeLog");
            modelBuilder.Entity <VATRateEntity>().ToTable("dir_VATRate").HasData(SeedData.GetVATRates());
            modelBuilder.Entity <UserTypeEntity>().ToTable("dir_UserType").HasData(SeedData.GetUserTypes());
            modelBuilder.Entity <AdviceScopeEntity>().ToTable("dir_AdviceScope").HasData(SeedData.GetAdviceScopes());
            modelBuilder.Entity <AdviceServiceEntity>().ToTable("dir_AdviceService").HasData(SeedData.GetAdviceServices());
            modelBuilder.Entity <LicenseCategoryEntity>().ToTable("dir_LicenseCategory").HasData(SeedData.GetLicenseCategories());

            //Custom mappings
            OrganisationMap.Map(modelBuilder);
            UserMap.Map(modelBuilder);
            RoleToUseCaseMap.Map(modelBuilder);
            CommissionTypeMap.Map(modelBuilder);
            CompanyMap.Map(modelBuilder);
            AuditLogMap.Map(modelBuilder);

            #endregion

            #region Client

            modelBuilder.Entity <ClientEntity>().ToTable("clt_Client");
            modelBuilder.Entity <PolicyEntity>().ToTable("clt_Policy");
            modelBuilder.Entity <PolicyTypeEntity>().ToTable("clt_PolicyType").HasData(SeedData.GetPolicyTypes());
            modelBuilder.Entity <PolicyProductTypeEntity>().ToTable("clt_PolicyProductType").HasData(SeedData.GetPolicyProductTypes());
            modelBuilder.Entity <PolicyProductEntity>().ToTable("clt_PolicyProduct");
            modelBuilder.Entity <ContactEntity>().ToTable("clt_Contact");
            modelBuilder.Entity <ContactTypeEntity>().ToTable("clt_ContactType").HasData(SeedData.GetContactTypes());
            modelBuilder.Entity <MarritalStatusEntity>().ToTable("clt_MarritalStatus").HasData(SeedData.GetMarritalStatus());
            modelBuilder.Entity <ClientTypeEntity>().ToTable("clt_ClientType").HasData(SeedData.GetClientTypes());
            modelBuilder.Entity <PolicyTypeCharacteristicEntity>().ToTable("clt_PolicyTypeCharacteristic");

            //Custom mappings
            PolicyMap.Map(modelBuilder);
            PolicyProductTypeMap.Map(modelBuilder);

            #endregion

            #region Commission

            modelBuilder.Entity <CommissionEntity>().ToTable("com_Commission");
            modelBuilder.Entity <CommissionErrorEntity>().ToTable("com_CommissionError");
            modelBuilder.Entity <CommissionStatementEntity>().ToTable("com_CommissionStatement");
            modelBuilder.Entity <CommissionStatementTemplateEntity>().ToTable("com_CommissionStatementTemplate");
            modelBuilder.Entity <CommissionTypeEntity>().ToTable("com_CommissionType");
            modelBuilder.Entity <CommissionAllocationEntity>().ToTable("com_CommissionAllocation");
            modelBuilder.Entity <CommissionAllocationPolicyEntity>().ToTable("com_CommissionAllocationPolicy");
            modelBuilder.Entity <CommissionEarningsTypeEntity>().ToTable("com_CommissionEarningsType").HasData(SeedData.GetCommissionEarningsTypes());
            modelBuilder.Entity <CommissionSplitRuleEntity>().ToTable("com_CommissionSplitRule");
            modelBuilder.Entity <CommissionSplitRulePolicyEntity>().ToTable("com_CommissionSplitRulePolicy");

            //Custom mappings
            CommissionMap.Map(modelBuilder);
            CommissionErrorMap.Map(modelBuilder);
            CommissionStatementMap.Map(modelBuilder);
            CommissionStatementTemplateMap.Map(modelBuilder);
            CommissionAllocationMap.Map(modelBuilder);
            CommissionSplitRuleMap.Map(modelBuilder);
            CommissionSplitRulePolicyMap.Map(modelBuilder);

            #endregion
        }