Esempio n. 1
0
        private async Task SeedDefaultPermissionsAsync(
            NetBooruContext context, CancellationToken cancellationToken)
        {
            if (await context.RoleClaims.AnyAsync(cancellationToken))
            {
                return;
            }

            _logger.LogDebug(
                "Seeding initial role claims as none could be found");

            var ownerRoleId = await context.Roles.Where(
                x => x.NormalizedName == PermissionConfiguration.OwnerRole)
                              .Select(x => x.Id)
                              .SingleAsync(cancellationToken);

            foreach (var permission in PermissionConfiguration.Permissions)
            {
                _ = context.RoleClaims.Add(new IdentityRoleClaim <ulong>
                {
                    ClaimType = permission.Claim,
                    RoleId    = ownerRoleId
                });
            }

            _ = await context.SaveChangesAsync(cancellationToken);

            _logger.LogDebug("Done seeding initial role claims");
        }
Esempio n. 2
0
        public async Task <IActionResult> Upload(
            IFormFile file,
            string[]?initialTags)
        {
            _logger.LogInformation("Handling file upload");

            var user = await _userManager.FindByIdAsync(User.FindFirstValue(ClaimTypes.NameIdentifier));

            var upload = await _uploadService.UploadFileAsync(file);

            // TODO: Generate Metadata
            // TODO: Tokenize supplied tags and apply
            var newPost = new Post
            {
                Uploader = user,
                Hash     = upload.Hash,
                Metadata = new PostMetadata
                {
                    MimeType = upload.MediaType
                }
            };

            _ = _dbContext.Posts.Add(newPost);
            _ = await _dbContext.SaveChangesAsync();

            // TODO: Less magic thx
            return(Created("http://localhost:5000/Post/" + newPost.Id, true));
        }
Esempio n. 3
0
        private async Task SeedDefaultRolesAsync(
            NetBooruContext context, CancellationToken cancellationToken)
        {
            if (await context.RoleClaims.AnyAsync(cancellationToken))
            {
                return;
            }

            _logger.LogDebug(
                "Seeding initial roles as none could be found");

            // If not logged in, the Anonymous virtual user they occupy will be
            // in this role.
            _ = context.Roles.Add(new Role()
            {
                Id             = 1,
                Name           = PermissionConfiguration.AnonymousRole,
                NormalizedName = PermissionConfiguration.AnonymousRole,
                Deletable      = false,
                Locked         = false,
                Color          = 0xBBBBBB
            });

            // Has no permissions, no matter what.
            _ = context.Roles.Add(new Role()
            {
                Id             = 3,
                Name           = PermissionConfiguration.BannedRole,
                NormalizedName = PermissionConfiguration.BannedRole,
                Deletable      = false,
                Locked         = true,
                Color          = 0x996666
            });

            // Has all permissions, no matter what.
            _ = context.Roles.Add(new Role()
            {
                Id             = 2,
                Name           = PermissionConfiguration.OwnerRole,
                NormalizedName = PermissionConfiguration.OwnerRole,
                Deletable      = false,
                Locked         = true,
                Color          = 0xFFFFFF
            });

            _ = await context.SaveChangesAsync(cancellationToken);

            _logger.LogDebug("Done seeding initial roles");
        }
Esempio n. 4
0
        private async Task SeedDefaultUsersAsync(
            NetBooruContext context, CancellationToken cancellationToken)
        {
            if (await context.Users.AnyAsync(cancellationToken))
            {
                return;
            }

            _logger.LogDebug(
                "Seeding initial users as none could be found");

            _ = context.Users.Add(new User
            {
                Id                 = 1,
                UserName           = PermissionConfiguration.AnonymousRole,
                NormalizedUserName = PermissionConfiguration.AnonymousRole.ToUpper(),
            });

            _ = await context.SaveChangesAsync(cancellationToken);

            _logger.LogDebug("Done seeding initial users");
        }