Showing 10 changed files with 94 additions and 173 deletions
-13
lib/Gitprep.pm
... ...
@@ -8,7 +8,6 @@ use Gitprep::Git;
8 8
 use DBIx::Custom;
9 9
 use Validator::Custom;
10 10
 use Encode qw/encode decode/;
11
-use Mojo::JSON;
12 11
 use Gitprep::API;
13 12
 use Carp 'croak';
14 13
 use Gitprep::RepManager;
... ...
@@ -86,18 +85,6 @@ sub startup {
86 85
   ];
87 86
   $dbi->create_model($_) for @$models;
88 87
 
89
-  # Fiter
90
-  $dbi->register_filter(json => sub {
91
-    my $value = shift;
92
-    
93
-    if (ref $value) {
94
-      return decode('UTF-8', Mojo::JSON->new->encode($value));
95
-    }
96
-    else {
97
-      return Mojo::JSON->new->decode(encode('UTF-8', $value));
98
-    }
99
-  });
100
-  
101 88
   # Validator
102 89
   my $validator = Validator::Custom->new;
103 90
   $self->validator($validator);
+23 -56
lib/Gitprep/API.pm
... ...
@@ -3,7 +3,6 @@ use Mojo::Base -base;
3 3
 
4 4
 use Carp ();
5 5
 use File::Basename ();
6
-use Mojo::JSON;
7 6
 use Encode qw/encode decode/;
8 7
 use Digest::MD5 'md5_hex';
9 8
 
... ...
@@ -12,19 +11,16 @@ sub dirname { File::Basename::dirname(@_) }
12 11
 
13 12
 has 'cntl';
14 13
 
14
+sub app { shift->cntl->app }
15
+
15 16
 sub admin_user {
16 17
   my $self = shift;
17 18
   
18
-  # DBI
19
-  my $dbi = $self->cntl->app->dbi;
20
-  
21 19
   # Admin user
22
-  my $users = $dbi->model('user')->select->filter('config' => 'json')->all;
23
-  for my $user (@$users) {
24
-    return $user->{id} if $user->{config}{admin};
25
-  }
20
+  my $admin_user = $self->app->dbi->model('user')
21
+    ->select('id', where => {admin => 1})->value;
26 22
   
27
-  return;
23
+  return $admin_user;
28 24
 }
29 25
 
30 26
 sub encrypt_password {
... ...
@@ -54,14 +50,10 @@ sub new {
54 50
 sub exists_admin {
55 51
   my $self = shift;
56 52
  
57
-  my $users = $self->cntl->app->dbi->model('user')->select(
58
-    ['id', 'config'],
59
-    append => 'order by id'
60
-  )->filter(config => 'json')->all;
53
+  my $row = $self->app->dbi->model('user')
54
+    ->select(where => {admin => 1})->one;
61 55
 
62
-  my $exists = grep { $_->{config}{admin} } @$users;
63
-  
64
-  return $exists;
56
+  return $row ? 1 : 0;;
65 57
 }
66 58
 
67 59
 sub root_ns {
... ...
@@ -75,18 +67,11 @@ sub root_ns {
75 67
 sub is_admin {
76 68
   my ($self, $user) = @_;
77 69
   
78
-  # Controler
79
-  my $c = $self->cntl;
80
-  
81
-  # DBI
82
-  my $dbi = $c->app->dbi;
83
-  
84 70
   # Check admin
85
-  my $row = $dbi->model('user')->select('config', id => $user)->one;
86
-  return unless $row;
87
-  my $config = $self->json($row->{config});
71
+  my $is_admin = $self->app->dbi->model('user')
72
+    ->select('admin', id => $user)->value;
88 73
   
89
-  return $config->{admin};
74
+  return $is_admin;
90 75
 }
91 76
 
92 77
 sub logined_admin {
... ...
@@ -101,18 +86,6 @@ sub logined_admin {
101 86
   return $self->is_admin($user) && $self->logined;
102 87
 }
103 88
 
104
-
105
-sub json {
106
-  my ($self, $value) = @_;
107
-  
108
-  if (ref $value) {
109
-    return decode('UTF-8', Mojo::JSON->new->encode($value));
110
-  }
111
-  else {
112
-    return Mojo::JSON->new->decode(encode('UTF-8', $value));
113
-  }
114
-}
115
-
116 89
 sub logined {
117 90
   my $self = shift;
118 91
   
... ...
@@ -124,22 +97,21 @@ sub logined {
124 97
   my $password = $c->session('password');
125 98
   return unless defined $password;
126 99
   
127
-  my $row = $dbi->model('user')->select('config', id => $user)->one;
128
-  return unless $row;
129
-  my $config = $self->json($row->{config});
100
+  my $correct_password
101
+    = $dbi->model('user')->select('password', id => $user)->value;
102
+  return unless defined $correct_password;
130 103
   
131
-  return $password eq $config->{password};
104
+  return $password eq $correct_password;
132 105
 }
133 106
 
134 107
 sub users {
135 108
   my $self = shift;
136 109
  
137
-  my $users = $self->cntl->app->dbi->model('user')->select(
138
-    ['id', 'config'],
110
+  my $users = $self->app->dbi->model('user')->select(
111
+    'id',
112
+    where => [':admin{<>}',{admin => 1}],
139 113
     append => 'order by id'
140
-  )->filter(config => 'json')->all;
141
-
142
-  @$users = grep { ! $_->{config}{admin} } @$users;
114
+  )->all;
143 115
   
144 116
   return $users;
145 117
 }
... ...
@@ -157,16 +129,11 @@ sub params {
157 129
 sub default_branch {
158 130
   my ($self, $user, $project) = @_;
159 131
   
160
-  my $c = $self->cntl;
161
-  my $dbi = $c->app->dbi;
162
-  my $row = $dbi->model('project')
163
-    ->select('config', id => [$user, $project])->one;
164
-  return unless $row;
165
-  
166
-  my $config = $self->json($row->{config});
167
-
132
+  my $default_branch = $self->app->dbi->model('project')
133
+    ->select('default_branch', id => [$user, $project])
134
+    ->value;
168 135
   
169
-  return $config->{default_branch};
136
+  return $default_branch;
170 137
 }
171 138
 
172 139
 1;
+35 -63
lib/Gitprep/RepManager.pm
... ...
@@ -4,8 +4,8 @@ use Mojo::Base -base;
4 4
 use Carp 'croak';
5 5
 use File::Copy 'move';
6 6
 use File::Path qw/mkpath rmtree/;
7
-use Mojo::JSON;
8 7
 use File::Temp ();
8
+use Encode 'encode';
9 9
 
10 10
 has 'app';
11 11
 
... ...
@@ -18,22 +18,21 @@ sub members {
18 18
   # Projects
19 19
   my $projects = $self->app->dbi
20 20
     ->model('project')
21
-    ->select(['user_id', 'name', 'config'])
22
-    ->filter(config => 'json')
21
+    ->select([qw/user_id name original_user original_project/])
23 22
     ->all;
24 23
   
25 24
   # Members
26 25
   my $members = [];
27 26
   for my $project (@$projects) {
28
-    $project->{config}{original_user} = ''
29
-      unless defined $project->{config}{original_user};
27
+    $project->{original_user} = ''
28
+      unless defined $project->{original_user};
30 29
     
31
-    $project->{config}{original_project} = ''
32
-      unless defined $project->{config}{original_project};
30
+    $project->{original_project} = ''
31
+      unless defined $project->{original_project};
33 32
     
34 33
     push @$members, {id => $project->{user_id}, project => $project->{name}}
35
-      if $project->{config}{original_user} eq $user
36
-        && $project->{config}{original_project} eq $project_name;
34
+      if $project->{original_user} eq $user
35
+        && $project->{original_project} eq $project_name;
37 36
   }
38 37
 
39 38
   return $members;
... ...
@@ -120,13 +119,12 @@ sub original_project {
120 119
   
121 120
   my $dbi = $self->app->dbi;
122 121
   
123
-  my $config = $dbi->model('project')
124
-    ->select('config', id => [$user, $project])
125
-    ->filter(config => 'json')
122
+  my $original_project = $dbi->model('project')
123
+    ->select('original_project', id => [$user, $project])
126 124
     ->value;
127
-  return unless $config;
125
+  return unless defined $original_project;
128 126
   
129
-  return $config->{original_project};
127
+  return $original_project;
130 128
 }
131 129
 
132 130
 sub original_user {
... ...
@@ -134,13 +132,12 @@ sub original_user {
134 132
   
135 133
   my $dbi = $self->app->dbi;
136 134
   
137
-  my $config = $dbi->model('project')
138
-    ->select('config', id => [$user, $project])
139
-    ->filter(config => 'json')
135
+  my $original_user = $dbi->model('project')
136
+    ->select('original_user', id => [$user, $project])
140 137
     ->value;
141
-  return unless $config;
138
+  return unless defined $original_user;
142 139
   
143
-  return $config->{original_user};
140
+  return $original_user;
144 141
 }
145 142
 
146 143
 sub _delete_db_user {
... ...
@@ -305,7 +302,7 @@ sub setup_database {
305 302
     my $sql = <<"EOS";
306 303
 create table user (
307 304
   row_id integer primary key autoincrement,
308
-  id not null unique,
305
+  id not null unique default ''
309 306
 );
310 307
 EOS
311 308
     $dbi->execute($sql);
... ...
@@ -313,16 +310,18 @@ EOS
313 310
 
314 311
   # Create usert columns
315 312
   my $user_columns = [
316
-    "config not null default ''",
313
+    "admin not null default '0'",
314
+    "password not null default ''",
315
+    "salt not null default ''"
317 316
   ];
318 317
   for my $column (@$user_columns) {
319 318
     eval { $dbi->execute("alter table user add column $column") };
320 319
   }
321 320
   
322 321
   # Check user table
323
-  eval { $dbi->select(['config'], table => 'user') };
322
+  eval { $dbi->select([qw/row_id id admin password salt/], table => 'user') };
324 323
   if ($@) {
325
-    my $error = "Can't create user table properly";
324
+    my $error = "Can't create user table properly: $@";
326 325
     $self->app->log->error($error);
327 326
     croak $error;
328 327
   }
... ...
@@ -342,37 +341,29 @@ EOS
342 341
   
343 342
   # Create Project columns
344 343
   my $project_columns = [
345
-    "config not null default ''",
344
+    "default_branch not null default 'master'",
345
+    "original_user not null default ''",
346
+    "original_project not null default ''"
346 347
   ];
347 348
   for my $column (@$project_columns) {
348 349
     eval { $dbi->execute("alter table project add column $column") };
349 350
   }
350 351
 
351 352
   # Check project table
352
-  eval { $dbi->select(['config'], table => 'project') };
353
+  eval { $dbi->select([qw/default_branch original_user original_project/], table => 'project') };
353 354
   if ($@) {
354
-    my $error = "Can't create project table properly";
355
+    my $error = "Can't create project table properly: $@";
355 356
     $self->app->log->error($error);
356 357
     croak $error;
357 358
   }
358 359
 }
359 360
 
360 361
 sub _create_project {
361
-  my ($self, $user, $project, $new_config) = @_;
362
-  $new_config ||= {};
363
-  
364
-  # Config
365
-  my $config = {default_branch => 'master'};
366
-  $config = {%$config, %$new_config};
367
-  my $config_json = Mojo::JSON->new->encode($config);
362
+  my ($self, $user, $project, $params) = @_;
363
+  $params ||= {};
368 364
   
369 365
   # Create project
370
-  $self->app->dbi->model('project')->insert(
371
-    {
372
-      config => $config_json,
373
-    },
374
-    id => [$user, $project]
375
-  );
366
+  $self->app->dbi->model('project')->insert($params, id => [$user, $project]);
376 367
 }
377 368
 
378 369
 sub _create_rep {
... ...
@@ -418,7 +409,7 @@ sub _create_rep {
418 409
     my $file = "$temp_rep/description";
419 410
     open my $fh, '>', $file
420 411
       or croak "Can't open $file: $!";
421
-    print $fh $description
412
+    print $fh encode('UTF-8', $description)
422 413
       or croak "Can't write $file: $!";
423 414
     close $fh;
424 415
   }
... ...
@@ -535,30 +526,11 @@ sub _rename_project {
535 526
   );
536 527
   
537 528
   # Rename related project
538
-  my $row_ids = $dbi->model('project')->select('row_id')->values;
539
-  for my $row_id (@$row_ids) {
540
-    my $config = $dbi->model('project')
541
-      ->select('config', where => {row_id => $row_id})
542
-      ->filter(config => 'json')
543
-      ->value;
544
-    
545
-    my $original_user = $config->{original_user};
546
-    $original_user = '' unless defined $original_user;
529
+  $dbi->model('project')->update(
530
+    {original_project => $renamed_project},
531
+    where => {original_user => $user, original_project => $project},
532
+  );
547 533
 
548
-    my $original_project = $config->{original_project};
549
-    $original_project = '' unless defined $original_project;
550
-    
551
-    if ($original_user eq $user
552
-      && $original_project eq $project)
553
-    {
554
-      $config->{original_project} = $renamed_project;
555
-      $dbi->model('project')->update(
556
-        {config => $config},
557
-        where => {row_id => $row_id},
558
-        filter => {config => 'json'}
559
-      );
560
-    }
561
-  }
562 534
 }
563 535
 
564 536
 sub _rename_rep {
+1 -2
templates/auto/_admin/user/create.html.ep
... ...
@@ -37,10 +37,9 @@
37 37
         = $api->encrypt_password($params->{password});
38 38
       $params->{password} = $password_encrypted;
39 39
       $params->{salt} = $salt;
40
-      my $config_json = $api->json($params);
41 40
       
42 41
       # Create user
43
-      eval { app->manager->create_user($id, {config => $config_json}) };
42
+      eval { app->manager->create_user($id, $params) };
44 43
       if ($@) {
45 44
         app->log->error($@);
46 45
         $errors = ['Internal Error'];
+1 -1
templates/auto/_admin/user/delete.html.ep
... ...
@@ -90,6 +90,6 @@
90 90
         </div>
91 91
       </form>
92 92
     </div>
93
-    <div class="text-center" style="margin-bottom:20px"><big><a href="/_admin/users">See Users</a></big></div>
93
+    <div class="text-center" style="margin-bottom:20px"><big><a href="/_admin/users">Users</a></big></div>
94 94
   </div>
95 95
   %= include '/include/footer';
+1 -1
templates/auto/_admin/users.html.ep
... ...
@@ -26,7 +26,7 @@
26 26
               <a href="#"><%= $uid %></a>
27 27
             </td>
28 28
             <td style="text-align:right">
29
-              <a class="btn btn-mini" href="<%= url_for("/reset-password?user=$uid") %>">Reset Password</a>
29
+              <a class="btn btn-mini" href="<%= url_for('/reset-password')->query(user => $uid, mode => 'admin') %>">Reset Password</a>
30 30
               <a class="btn btn-mini" href="<%= url_for("/_admin/user/delete?user=$uid") %>">Delete</a>
31 31
             </td>
32 32
           </tr>
+8 -11
templates/auto/_login.html.ep
... ...
@@ -23,17 +23,15 @@
23 23
       
24 24
       my ($id, $password) = @$values;
25 25
       
26
-      my $config_json
27
-        = $dbi->model('user')->select('config', id => $id)->value;
26
+      my $row
27
+        = $dbi->model('user')->select(['password', 'salt'], id => $id)->one;
28 28
       
29
-      return unless $config_json;
30
-      
31
-      my $config = $api->json($config_json);
29
+      return unless defined $password;
32 30
 
33 31
       my $is_valid = $api->check_password(
34 32
         $password,
35
-        $config->{salt},
36
-        $config->{password}
33
+        $row->{salt},
34
+        $row->{password}
37 35
       );
38 36
       
39 37
       return $is_valid;
... ...
@@ -59,10 +57,9 @@
59 57
       my $params = $vresult->data;
60 58
       my $id = $params->{id};
61 59
       my $password = $params->{password};
62
-      my $config_json = $self->app->dbi->model('user')->select('config', id => $id)->value;
63
-      my $config = $api->json($config_json);
64
-      my $password_encrypted = $config->{password};
65
-      my $admin = $config->{admin};
60
+      my $row = $self->app->dbi->model('user')->select(['admin', 'password'], id => $id)->one;
61
+      my $password_encrypted = $row->{password};
62
+      my $admin = $row->{admin};
66 63
       session(user => $id);
67 64
       session(password => $password_encrypted);
68 65
       
+3 -8
templates/auto/_start.html.ep
... ...
@@ -1,6 +1,4 @@
1 1
 <%
2
-  use Mojo::Util 'md5_sum';
3
-  
4 2
   # API
5 3
   my $api = gitprep_api;
6 4
   
... ...
@@ -15,9 +13,7 @@
15 13
     
16 14
     # Check existence admin user
17 15
     my $admin_user = app->dbi->model('user')->select(id => 'admin')->one;
18
-    if ($admin_user) {
19
-      $errors = ['admin user already exists'];
20
-    }
16
+    if (defined $admin_user) { $errors = ['admin user already exists'] }
21 17
     else {
22 18
       # Validation
23 19
       my $params = $api->params;
... ...
@@ -39,16 +35,15 @@
39 35
         
40 36
         # Valida parameters
41 37
         my $params = $vresult->data;
42
-        my $id = 'admin';
38
+        my $user = 'admin';
43 39
         $params->{admin} = 1;
44 40
         my ($password_encryped, $salt)
45 41
           = $api->encrypt_password($params->{password});
46 42
         $params->{password} = $password_encryped;
47 43
         $params->{salt} = $salt;
48
-        my $config_json = $api->json($params);
49 44
         
50 45
         # Create admin user
51
-        $self->app->dbi->model('user')->insert({id => $id, config => $config_json});
46
+        $self->app->dbi->model('user')->insert($params, id => $user);
52 47
         
53 48
         # Redirect
54 49
         $self->flash(admin_user_created => 1);
+21 -18
templates/auto/reset-password.html.ep
... ...
@@ -11,6 +11,7 @@
11 11
   
12 12
   # User
13 13
   my $user = param('user');
14
+  my $mode = param('mode') || '';
14 15
 
15 16
   # Reset password
16 17
   my $reset_password_conf_on;
... ...
@@ -71,28 +72,24 @@
71 72
         my ($password_encrypted, $salt)
72 73
           = $api->encrypt_password($valid_params->{password});
73 74
         
74
-        # Create admin user
75
+        # Reset password
75 76
         my $dbi = app->dbi;
77
+        my $count = $dbi->model('user')
78
+          ->update({password => $password_encrypted, salt => $salt}, id => $user);
76 79
         
77
-        my $config_json = $dbi->model('user')->select('config', id => $user)->value;
78
-        if (defined $config_json) {
79
-          my $config = $api->json($config_json);
80
-          $config->{password} = $password_encrypted;
81
-          $config->{salt} = $salt;
82
-          $self->app->dbi->model('user')->update({config => $config_json}, id => $user);
80
+        if ($count) {
81
+          # Redirect
82
+          my $messages = [];
83
+          push @$messages, "Success Reset Password: User $user password is changed";
84
+          if ($reset_password_conf_on) {
85
+            my $message = "Password is reseted. Don't forget"
86
+              . " to comment out reset_password option line from config file";
87
+            push @$messages, $message;
88
+          }
89
+          flash(messages => $messages);
90
+          $self->redirect_to(url_for->query(user => $user, mode => $mode));
83 91
         }
84 92
         else { $errors = ["User $user don't exists"] }
85
-        
86
-        # Redirect
87
-        my $messages = [];
88
-        push @$messages, "Success Reset Password: User $user password is changed";
89
-        if ($reset_password_conf_on) {
90
-          my $message = "Password is reseted. Don't forget"
91
-            . " to comment out reset_password option line from config file";
92
-          push @$messages, $message;
93
-        }
94
-        flash(messages => $messages);
95
-        $self->redirect_to(url_for->query(user => $user));
96 93
       }
97 94
       else { $errors = $vresult->messages }
98 95
     }
... ...
@@ -130,6 +127,9 @@
130 127
           <div>
131 128
             <b><%= $user %></b>
132 129
             %= hidden_field user => $user;
130
+            % if ($mode) {
131
+              %= hidden_field mode => $mode;
132
+            % }
133 133
           </div>
134 134
         </div>
135 135
         <div class="control-group">
... ...
@@ -146,6 +146,9 @@
146 146
         </div>
147 147
       </form>
148 148
     </div>
149
+    % if ($mode eq 'admin') {
150
+      <div class="text-center" style="margin-bottom:20px;font-size:120%"><a href="<%= url_for('_admin/users') %>">Users</a></div>
151
+    % }
149 152
   </div>
150 153
 
151 154
   %= include '/include/footer';
+1
templates/project-settings.html.ep
... ...
@@ -212,6 +212,7 @@
212 212
         Default Branch
213 213
         % my $branches = $git->branches($user, $project);
214 214
         % my $branch_names = [map { $_->{name} } @$branches];
215
+        % push @$branch_names, 'master' unless @$branch_names;
215 216
         %= select_field 'default_branch' => $branch_names, style => 'margin-top:5px';
216 217
       </div>
217 218
     </div>