denny/ShinyCMS

View on GitHub
bin/database/data/insert-fileserver-demo-data

Summary

Maintainability
Test Coverage
#!/usr/bin/env perl

# ===================================================================
# File:        bin/database/insert-fileserver-demo-data
# Project:    ShinyCMS
# Purpose:    Insert fileserver demo data via DBIC
#
# Author:    Denny de la Haye <2019@denny.me>
# Copyright (c) 2009-2019 Denny de la Haye
#
# ShinyCMS is free software; you can redistribute it and/or modify it
# under the terms of either the GPL 2.0 or the Artistic License 2.0
# ===================================================================

use strict;
use warnings;

# Load local helper lib and get connected schema object
use FindBin qw( $Bin );
use lib "$Bin/../../lib";
require 'helpers.pl';  ## no critic

my $schema = get_schema();


# Find out what year it is
my $year = DateTime->now->year;
my $next = $year + 1;
my $prev = $year - 1;

# Create some access groups
my $access1 = $schema->resultset( 'Access' )->find_or_create({
    access => 'Eternal',
});
my $access2 = $schema->resultset( 'Access' )->find_or_create({
    access => 'Expired',
});
my $access3 = $schema->resultset( 'Access' )->find_or_create({
    access => 'Unexpired',
});
my $access4 = $schema->resultset( 'Access' )->find_or_create({
    access => 'Exclusive',
});
# Create a user, given them active access to the Expired access level
my $first = $schema->resultset( 'User' )->find_or_create({
    username    => 'first',
    password    => 'stillhere',
    email       => 'first@example.com',
    admin_notes => 'Part of the demo/test data.',
});
$first->user_accesses->find_or_create({
    access  => $access2->id,
    expires => $next . '-12-31 12:34:56'
});
# Create a second user, give them active access to Eternal and Unexpired, expired access to Expired
my $user = $schema->resultset( 'User' )->find_or_create({
    username    => 'viewer',
    password    => 'changeme',
    email       => 'viewer@example.com',
    admin_notes => 'Part of the demo/test data.',
});
$user->user_accesses->find_or_create({
    access  => $access1->id,
});
$user->user_accesses->find_or_create({
    access  => $access2->id,
    expires => $prev . '-12-31 12:34:56'
});
$user->user_accesses->find_or_create({
    access  => $access3->id,
    expires => $next . '-12-31 12:34:56'
});

# Create a couple of access log entries
$user->file_accesses->create({
    access_group => 'Eternal',
    filepath     => 'dir-one',
    filename     => 'empty-file.txt',
    ip_address   => '10.10.10.10',
});
$user->file_accesses->create({
    access_group => 'Unexpired',
    filepath     => 'sub/sub/sub/dir',
    filename     => 'empty-too.txt',
    ip_address   => '10.20.30.40',
});