public static void SeedUserWithTwoRoles(this ExtraAuthorizeDbContext context, string userId = "userId")
        {
            var userStatus = RoleToPermissions.CreateRoleWithPermissions(
                "TestRole1", "TestRole1", new List <Permissions> {
                Permissions.StockRead
            }, context);

            userStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(userStatus.Result);
            userStatus = RoleToPermissions.CreateRoleWithPermissions(
                "TestRole2", "TestRole1", new List <Permissions> {
                Permissions.SalesSell
            }, context);
            userStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(userStatus.Result);

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

            roleStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(roleStatus.Result);
            roleStatus = UserToRole.AddRoleToUser(userId, "TestRole2", context);
            roleStatus.IsValid.ShouldBeTrue(userStatus.GetAllErrors());
            context.Add(roleStatus.Result);

            context.SaveChanges();
        }
Esempio n. 2
0
        /// <summary>
        /// This ensures there is a UserToRole linking the userId to the given roleName
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleName"></param>
        public void CheckAddRoleToUser(string userId, string roleName)
        {
            var status = UserToRole.AddRoleToUser(userId, roleName, _context);

            if (status.IsValid)
            {
                //we assume there is already a link to the role is the status wasn't valid
                _context.Add(status.Result);
            }
        }
        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();
        }