| File: | lib/OpenAPI/Backend/Base.pm |
| Coverage: | 76.3% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package OpenAPI::Backend::Base; | ||||||
| 2 | |||||||
| 3 | 23 23 23 | 170 227 180 | use strict; | ||||
| 4 | 23 23 23 | 242 82 162 | use warnings; | ||||
| 5 | |||||||
| 6 | #use Smart::Comments; | ||||||
| 7 | 23 23 23 | 251 70 355 | use SQL::Insert; | ||||
| 8 | |||||||
| 9 | my %DefaultRules = ( | ||||||
| 10 | Admin => [ | ||||||
| 11 | [ DELETE => 'model' ], | ||||||
| 12 | [ DELETE => 'model/~' ], | ||||||
| 13 | [ DELETE => 'model/~/~' ], | ||||||
| 14 | [ DELETE => 'model/~/~/~' ], | ||||||
| 15 | [ DELETE => 'model/~/~/~/~' ], | ||||||
| 16 | |||||||
| 17 | [ GET => 'model' ], | ||||||
| 18 | [ GET => 'model/~' ], | ||||||
| 19 | [ GET => 'model/~/~' ], | ||||||
| 20 | [ GET => 'model/~/~/~' ], | ||||||
| 21 | [ GET => 'model/~/~/~/~' ], | ||||||
| 22 | |||||||
| 23 | [ PUT => 'model' ], | ||||||
| 24 | [ PUT => 'model/~' ], | ||||||
| 25 | [ PUT => 'model/~/~' ], | ||||||
| 26 | [ PUT => 'model/~/~/~' ], | ||||||
| 27 | [ PUT => 'model/~/~/~/~' ], | ||||||
| 28 | |||||||
| 29 | [ POST => 'model' ], | ||||||
| 30 | [ POST => 'model/~' ], | ||||||
| 31 | [ POST => 'model/~/~' ], | ||||||
| 32 | [ POST => 'model/~/~/~' ], | ||||||
| 33 | [ POST => 'model/~/~/~/~' ], | ||||||
| 34 | |||||||
| 35 | [ DELETE => 'view' ], | ||||||
| 36 | [ DELETE => 'view/~' ], | ||||||
| 37 | [ DELETE => 'view/~/~' ], | ||||||
| 38 | [ DELETE => 'view/~/~/~' ], | ||||||
| 39 | [ DELETE => 'view/~/~/~/~' ], | ||||||
| 40 | |||||||
| 41 | [ GET => 'view' ], | ||||||
| 42 | [ GET => 'view/~' ], | ||||||
| 43 | [ GET => 'view/~/~' ], | ||||||
| 44 | [ GET => 'view/~/~/~' ], | ||||||
| 45 | [ GET => 'view/~/~/~/~' ], | ||||||
| 46 | |||||||
| 47 | [ PUT => 'view' ], | ||||||
| 48 | [ PUT => 'view/~' ], | ||||||
| 49 | [ PUT => 'view/~/~' ], | ||||||
| 50 | [ PUT => 'view/~/~/~' ], | ||||||
| 51 | [ PUT => 'view/~/~/~/~' ], | ||||||
| 52 | |||||||
| 53 | [ POST => 'view' ], | ||||||
| 54 | [ POST => 'view/~' ], | ||||||
| 55 | [ POST => 'view/~/~' ], | ||||||
| 56 | [ POST => 'view/~/~/~' ], | ||||||
| 57 | [ POST => 'view/~/~/~/~' ], | ||||||
| 58 | |||||||
| 59 | [ DELETE => 'role' ], | ||||||
| 60 | [ DELETE => 'role/~' ], | ||||||
| 61 | [ DELETE => 'role/~/~' ], | ||||||
| 62 | [ DELETE => 'role/~/~/~' ], | ||||||
| 63 | [ DELETE => 'role/~/~/~/~' ], | ||||||
| 64 | |||||||
| 65 | [ GET => 'role' ], | ||||||
| 66 | [ GET => 'role/~' ], | ||||||
| 67 | [ GET => 'role/~/~' ], | ||||||
| 68 | [ GET => 'role/~/~/~' ], | ||||||
| 69 | [ GET => 'role/~/~/~/~' ], | ||||||
| 70 | |||||||
| 71 | [ PUT => 'role' ], | ||||||
| 72 | [ PUT => 'role/~' ], | ||||||
| 73 | [ PUT => 'role/~/~' ], | ||||||
| 74 | [ PUT => 'role/~/~/~' ], | ||||||
| 75 | [ PUT => 'role/~/~/~/~' ], | ||||||
| 76 | |||||||
| 77 | [ POST => 'role' ], | ||||||
| 78 | [ POST => 'role/~' ], | ||||||
| 79 | [ POST => 'role/~/~' ], | ||||||
| 80 | [ POST => 'role/~/~/~' ], | ||||||
| 81 | [ POST => 'role/~/~/~/~' ], | ||||||
| 82 | |||||||
| 83 | [ DELETE => 'action' ], | ||||||
| 84 | [ DELETE => 'action/~' ], | ||||||
| 85 | [ DELETE => 'action/~/~' ], | ||||||
| 86 | [ DELETE => 'action/~/~/~' ], | ||||||
| 87 | [ DELETE => 'action/~/~/~/~' ], | ||||||
| 88 | |||||||
| 89 | [ GET => 'action' ], | ||||||
| 90 | [ GET => 'action/~' ], | ||||||
| 91 | [ GET => 'action/~/~' ], | ||||||
| 92 | [ GET => 'action/~/~/~' ], | ||||||
| 93 | [ GET => 'action/~/~/~/~' ], | ||||||
| 94 | |||||||
| 95 | [ PUT => 'action' ], | ||||||
| 96 | [ PUT => 'action/~' ], | ||||||
| 97 | [ PUT => 'action/~/~' ], | ||||||
| 98 | [ PUT => 'action/~/~/~' ], | ||||||
| 99 | [ PUT => 'action/~/~/~/~' ], | ||||||
| 100 | |||||||
| 101 | [ POST => 'action' ], | ||||||
| 102 | [ POST => 'action/~' ], | ||||||
| 103 | [ POST => 'action/~/~' ], | ||||||
| 104 | [ POST => 'action/~/~/~' ], | ||||||
| 105 | [ POST => 'action/~/~/~/~' ], | ||||||
| 106 | |||||||
| 107 | [ POST => 'admin/~' ], | ||||||
| 108 | ], | ||||||
| 109 | ); | ||||||
| 110 | |||||||
| 111 | sub state { | ||||||
| 112 | 602 | 0 | 20679 | $_[0]->{dbh}->state; | |||
| 113 | } | ||||||
| 114 | |||||||
| 115 | sub disconnect { | ||||||
| 116 | 0 | 0 | 0 | $_[0]->{dbh}->disconnect; | |||
| 117 | 0 | 0 | $_[0]->{dbh} = undef; | ||||
| 118 | } | ||||||
| 119 | |||||||
| 120 | sub add_default_roles { | ||||||
| 121 | 1 | 0 | 14 | my ($self, $user, $admin_password) = @_; | |||
| 122 | 1 | 14 | if (!defined $admin_password) { | ||||
| 123 | 0 | 0 | warn "No password specified.\n"; | ||||
| 124 | } | ||||||
| 125 | 1 | 20 | my $sql = <<"_EOC_"; | ||||
| 126 | insert into $user._roles (name, description, login, password) | ||||||
| 127 | values ('Admin', 'Administrator', 'password', '$admin_password'); | ||||||
| 128 | |||||||
| 129 | insert into $user._roles (name, description, login) | ||||||
| 130 | values ('Public', 'Anonymous', 'anonymous'); | ||||||
| 131 | _EOC_ | ||||||
| 132 | 1 | 19 | while (my ($role, $rules) = each %DefaultRules) { | ||||
| 133 | 1 | 13 | for my $rule (@$rules) { | ||||
| 134 | 81 | 1933 | my $insert = SQL::Insert->new("$user._access_rules") | ||||
| 135 | ->cols(qw< role method url >) | ||||||
| 136 | ->values("'$role'", "'$rule->[0]'", "'/=/$rule->[1]'"); | ||||||
| 137 | 81 | 262 | $sql .= $insert; | ||||
| 138 | } | ||||||
| 139 | } | ||||||
| 140 | 1 | 38 | $self->do($sql); | ||||
| 141 | } | ||||||
| 142 | |||||||
| 143 | sub add_user { | ||||||
| 144 | 1 | 0 | 16 | my ($self, $user, $admin_password) = @_; | |||
| 145 | 1 | 46 | my $retval = $self->do(<<"_EOC_"); | ||||
| 146 | create table $user._models ( | ||||||
| 147 | id serial primary key, | ||||||
| 148 | name text unique not null, | ||||||
| 149 | table_name text unique not null, | ||||||
| 150 | description text | ||||||
| 151 | ); | ||||||
| 152 | |||||||
| 153 | create table $user._columns ( | ||||||
| 154 | id serial primary key, | ||||||
| 155 | name text not null, | ||||||
| 156 | type text not null, | ||||||
| 157 | table_name text not null, | ||||||
| 158 | "default" text, | ||||||
| 159 | label text, | ||||||
| 160 | unique(table_name, name) | ||||||
| 161 | ); | ||||||
| 162 | |||||||
| 163 | create table $user._roles ( | ||||||
| 164 | name text primary key, | ||||||
| 165 | parentRole integer default 0, -- a column reference to $user._roles itself. 0 means no parent | ||||||
| 166 | password text, | ||||||
| 167 | login text not null, | ||||||
| 168 | description text not null | ||||||
| 169 | ); | ||||||
| 170 | |||||||
| 171 | create table $user._access_rules ( | ||||||
| 172 | id serial primary key, | ||||||
| 173 | role text not null, | ||||||
| 174 | method varchar(10) not null, | ||||||
| 175 | url text not null | ||||||
| 176 | ); | ||||||
| 177 | |||||||
| 178 | create table $user._views ( | ||||||
| 179 | id serial primary key, | ||||||
| 180 | name text unique not null, | ||||||
| 181 | definition text unique not null, | ||||||
| 182 | createdate timestamp with time zone default current_timestamp, | ||||||
| 183 | updatedate timestamp with time zone default current_timestamp, | ||||||
| 184 | description text | ||||||
| 185 | ); | ||||||
| 186 | _EOC_ | ||||||
| 187 | #$retval += 0; | ||||||
| 188 | ### $admin_password | ||||||
| 189 | 1 | 41 | $self->add_default_roles($user, $admin_password); | ||||
| 190 | 1 | 29 | return $retval; | ||||
| 191 | } | ||||||
| 192 | |||||||
| 193 | 1; | ||||||
| 194 | |||||||