public static void SeedCacheRole(this ExtraAuthorizeDbContext context, bool hasCache2, string userId = "userId")
        {
            var permissions = new List <Permissions> {
                Permissions.Cache1
            };

            if (hasCache2)
            {
                permissions.Add(Permissions.Cache2);
            }
            var userStatus = RoleToPermissions.CreateRoleWithPermissions(
                CacheRoleService.CacheRoleName, CacheRoleService.CacheRoleName, permissions, context);

            userStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(userStatus.Result);

            context.SaveChanges();
        }
Beispiel #2
0
        /// <summary>
        /// This toggles whether the <see cref="Permissions.Cache2"/> permission is in the <see cref="CacheRoleName"/>.
        /// This causes the <see cref="ExtraAuthorizeDbContext"/> to update the TimeStore with the time this change happens.
        /// Then the <see cref="CodeCalledInStartup.AuthCookieValidate"/> will compare the users lastUpdated time which will
        /// cause a recalc of the logged-in user's permission claim.
        /// </summary>
        public void ToggleCacheRole()
        {
            var hasCache2Permission = _context.Find <RoleToPermissions>(CacheRoleName)
                                      .PermissionsInRole.Any(x => x == Permissions.Cache2);
            var updatedPermissions = new List <Permissions> {
                Permissions.Cache1
            };

            if (!hasCache2Permission)
            {
                updatedPermissions.Add(Permissions.Cache2);
            }

            var authUserHelper = new ExtraAuthUsersSetup(_context);

            authUserHelper.UpdateRole(CacheRoleName, $"Has {updatedPermissions.Count} permissions.", updatedPermissions);
            _context.SaveChanges();
        }
Beispiel #3
0
        public void TestGetValueFromStore()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, null))
            {
                context.Database.EnsureCreated();
                context.AddUpdateValue("test", (long)1234);
                context.SaveChanges();

                //ATTEMPT
                var result = context.GetValueFromStore("test");

                //VERIFY
                result.ShouldEqual((long)1234);
            }
        }
        public void TestAddRoleNotTrigger()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, new AuthChanges()))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var rolToPer = RoleToPermissions.CreateRoleWithPermissions
                                   ("test", "test", new List <Permissions> {
                    Permissions.AccessAll
                }, context).Result;
                context.Add(rolToPer);
                context.SaveChanges();

                //VERIFY
                context.TimeStores.Count().ShouldEqual(0);
                context.RolesToPermissions.Count().ShouldEqual(1);
            }
        }
        public void TestAddRoleNotTrigger()
        {
            //SETUP
            var fakeCache = new FakeSimpleCache();
            var options   = SqliteInMemory.CreateOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, fakeCache))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                var rolToPer = RoleToPermissions.CreateRoleWithPermissions
                                   ("test", "test", new List <Permissions> {
                    Permissions.AccessAll
                }, context).Result;
                context.Add(rolToPer);
                context.SaveChanges();

                //VERIFY
                fakeCache.AddOrUpdateCalled.ShouldBeFalse();
                context.RolesToPermissions.Count().ShouldEqual(1);
            }
        }
Beispiel #6
0
        public void GetValueFromStorePerformance()
        {
            //SETUP
            var options = this.CreateUniqueClassOptions <ExtraAuthorizeDbContext>();

            using (var context = new ExtraAuthorizeDbContext(options, null))
            {
                context.Database.EnsureCreated();
                context.AddUpdateValue("test", (long)1234);
                context.SaveChanges();

                const int numTimes = 1000;
                //ATTEMPT

                using (new TimeThings(_output, "EFCore - startup", 10))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var result = context.GetValueFromStore("test");
                    }
                }
                using (new TimeThings(_output, "EFCore", numTimes))
                {
                    for (int i = 0; i < numTimes; i++)
                    {
                        var result = context.GetValueFromStore("test");
                    }
                }
                using (new TimeThings(_output, "EFCore", numTimes))
                {
                    for (int i = 0; i < numTimes; i++)
                    {
                        var result = context.GetValueFromStore("test");
                    }
                }
            }
        }
        public static void SeedUserWithDefaultPermissions(this ExtraAuthorizeDbContext context,
                                                          PaidForModules modules = PaidForModules.None, string userId = "userId")
        {
            var defaultPermissions = new List <Permissions> {
                Permissions.StockRead, Permissions.Feature1Access
            };

            var roleStatus = RoleToPermissions.CreateRoleWithPermissions(
                "TestRole1", "TestRole1", defaultPermissions, context);

            roleStatus.IsValid.ShouldBeTrue(roleStatus.GetAllErrors());
            context.Add(roleStatus.Result);

            var moduleUser = new ModulesForUser(userId, modules);

            context.Add(moduleUser);

            var userStatus = UserToRole.AddRoleToUser(userId, "TestRole1", context);

            userStatus.IsValid.ShouldBeTrue(roleStatus.GetAllErrors());
            context.Add(userStatus.Result);

            context.SaveChanges();
        }