Showing 9 changed files with 172 additions and 71 deletions
+4
gitprep.conf
... ...
@@ -13,6 +13,10 @@
13 13
 ;;; Tags limit (default:1000)
14 14
 ;tags_limit=1000
15 15
 
16
+;;; If you set this value to 1, user need login to see git repositories.
17
+;;; Default is 0
18
+;need_login_always=1
19
+
16 20
 [admin]
17 21
 ;;; If you forget admin password,
18 22
 ;;; set this value to 1 and access /reset-password page.
+76 -64
lib/Gitprep.pm
... ...
@@ -31,8 +31,10 @@ sub startup {
31 31
   $self->plugin('INIConfig', {ext => 'conf'});
32 32
   
33 33
   # Config file for developper
34
-  my $my_conf_file = $self->home->rel_file('gitprep.my.conf');
35
-  $self->plugin('INIConfig', {file => $my_conf_file}) if -f $my_conf_file;
34
+  unless ($ENV{GITPREP_NO_MYCONFIG}) {
35
+    my $my_conf_file = $self->home->rel_file('gitprep.my.conf');
36
+    $self->plugin('INIConfig', {file => $my_conf_file}) if -f $my_conf_file;
37
+  }
36 38
   
37 39
   # Listen
38 40
   my $conf = $self->config;
... ...
@@ -145,90 +147,100 @@ sub startup {
145 147
         
146 148
         my $api = $self->gitprep_api;
147 149
         
148
-        # Admin page authentication
150
+        # Authentication
149 151
         {
150 152
           my $path = $self->req->url->path->parts->[0] || '';
151
-
153
+          
154
+          # Admin
152 155
           if ($path eq '_admin' && !$api->logined_admin) {
153 156
             $self->redirect_to('/');
154 157
             return;
155 158
           }
159
+          # Need login always
160
+          elsif ($self->config->{basic}{need_login_always} && !$api->logined) {
161
+            if ($path ne '_start' && $path ne 'reset-password' && $path ne '_login') {
162
+              $self->redirect_to('/_login');
163
+              return;
164
+            }
165
+          }
156 166
         }
157 167
         
158 168
         return 1; 
159 169
       });
170
+      
171
+      # Auto routes
160 172
       $self->plugin('AutoRoute', route => $r);
161
-    }
162 173
 
163
-    # Custom routes
164
-    {
165
-      my $id_re = qr/[a-zA-Z0-9_-]+/;
166
-      
167
-      # User
168
-      my $r = $r->route('/:user', user => $id_re);
169
-      {
170
-        # Home
171
-        $r->get('/' => template '/user');
172
-        
173
-        # Settings
174
-        $r->get('/_settings' => template '/user-settings');
175
-      }
176
-      
177
-      # Project
174
+      # Custom routes
178 175
       {
179
-        my $r = $r->route('/:project', project => $id_re);
180
-        
181
-        # Home
182
-        $r->get('/' => template '/project');
176
+        my $id_re = qr/[a-zA-Z0-9_-]+/;
183 177
         
184
-        # Commit
185
-        $r->get('/commit/*diff' => template '/commit');
186
-        
187
-        # Commits
188
-        $r->get('/commits/*rev_file' => template '/commits');
178
+        # User
179
+        my $r = $r->route('/:user', user => $id_re);
180
+        {
181
+          # Home
182
+          $r->get('/' => template '/user');
183
+          
184
+          # Settings
185
+          $r->get('/_settings' => template '/user-settings');
186
+        }
189 187
         
190
-        # Branches
191
-        $r->any('/branches/*base_branch' => {base_branch => undef} => template '/branches');
188
+        # Project
189
+        {
190
+          my $r = $r->route('/:project', project => $id_re);
191
+          
192
+          # Home
193
+          $r->get('/' => template '/project');
194
+          
195
+          # Commit
196
+          $r->get('/commit/*diff' => template '/commit');
197
+          
198
+          # Commits
199
+          $r->get('/commits/*rev_file' => template '/commits');
200
+          
201
+          # Branches
202
+          $r->any('/branches/*base_branch' => {base_branch => undef} => template '/branches');
192 203
 
193
-        # Tags
194
-        $r->get('/tags' => template '/tags');
204
+          # Tags
205
+          $r->get('/tags' => template '/tags');
195 206
 
196
-        # Tree
197
-        $r->get('/tree/*rev_dir' => template '/tree');
198
-        
199
-        # Blob
200
-        $r->get('/blob/*rev_file' => template '/blob');
207
+          # Tree
208
+          $r->get('/tree/*rev_dir' => template '/tree');
209
+          
210
+          # Blob
211
+          $r->get('/blob/*rev_file' => template '/blob');
201 212
 
202
-        # Raw
203
-        $r->get('/raw/*rev_file' => template '/raw');
213
+          # Raw
214
+          $r->get('/raw/*rev_file' => template '/raw');
204 215
 
205
-        # Blame
206
-        $r->get('/blame/*rev_file' => template '/blame');
207
-        
208
-        # Archive
209
-        $r->get('/archive/(*rev).tar.gz' => template '/archive')->to(archive_type => 'tar');
210
-        $r->get('/archive/(*rev).zip' => template '/archive')->to(archive_type => 'zip' );
211
-        
212
-        # Compare
213
-        $r->get('/compare/(*rev1)...(*rev2)' => template '/compare');
214
-        
215
-        # Settings
216
-        $r->any('/settings' => template '/settings');
217
-        
218
-        # Fork
219
-        $r->any('/fork' => template '/fork');
216
+          # Blame
217
+          $r->get('/blame/*rev_file' => template '/blame');
218
+          
219
+          # Archive
220
+          $r->get('/archive/(*rev).tar.gz' => template '/archive')->to(archive_type => 'tar');
221
+          $r->get('/archive/(*rev).zip' => template '/archive')->to(archive_type => 'zip' );
222
+          
223
+          # Compare
224
+          $r->get('/compare/(*rev1)...(*rev2)' => template '/compare');
225
+          
226
+          # Settings
227
+          $r->any('/settings' => template '/settings');
228
+          
229
+          # Fork
230
+          $r->any('/fork' => template '/fork');
220 231
 
221
-        # Network
222
-        $r->get('/network' => template '/network');
232
+          # Network
233
+          $r->get('/network' => template '/network');
223 234
 
224
-        # Network Graph
225
-        $r->get('/network/graph/(*rev1)...(*rev2_abs)' => template '/network/graph');
235
+          # Network Graph
236
+          $r->get('/network/graph/(*rev1)...(*rev2_abs)' => template '/network/graph');
226 237
 
227
-        # Import branch
228
-        $r->any('/import-branch/:remote_user/:remote_project' => template '/import-branch');
229
-        
230
-        # Get branches and tags
231
-        $r->get('/api/revs' => template '/api/revs');
238
+          # Import branch
239
+          $r->any('/import-branch/:remote_user/:remote_project' => template '/import-branch');
240
+          
241
+          # Get branches and tags
242
+          $r->get('/api/revs' => template '/api/revs');
243
+        }
232 244
       }
233 245
     }
234 246
   }
+1 -1
templates/auto/_login.html.ep
... ...
@@ -87,7 +87,7 @@
87 87
 
88 88
 % layout 'common', title => 'Sign in';
89 89
 
90
-  <!-- Login Page -->
90
+  <!-- Login page -->
91 91
   
92 92
   %= include '/include/header';
93 93
 
+1 -1
templates/auto/_start.html.ep
... ...
@@ -61,7 +61,7 @@
61 61
 % layout 'common', title => 'Start page';
62 62
 
63 63
   %= include '/include/header';
64
-
64
+  <!-- Start page -->
65 65
   <div class="container">
66 66
     % if ($errors) {
67 67
       <div class="alert alert-error">
+2
templates/auto/reset-password.html.ep
... ...
@@ -87,6 +87,8 @@
87 87
 % layout 'common', title => 'Reset password';
88 88
 
89 89
   %= include '/include/header';
90
+  
91
+  <!-- Reset password page -->
90 92
 
91 93
   <div class="container">
92 94
     % if (my $messages = flash('messages')) {
+2
xt/basic.t
... ...
@@ -15,6 +15,8 @@ $ENV{GITPREP_DB_FILE} = "$FindBin::Bin/basic.db";
15 15
 # Test Repository home
16 16
 $ENV{GITPREP_REP_HOME} = "$FindBin::Bin/../../gitprep_t_rep_home";
17 17
 
18
+$ENV{GITPREP_NO_MYCONFIG} = 1;
19
+
18 20
 use Gitprep;
19 21
 
20 22
 my $app = Gitprep->new;
+3
xt/import_rep.t
... ...
@@ -18,6 +18,9 @@ my $db_file = $ENV{GITPREP_DB_FILE} = "$FindBin::Bin/import_rep.db";
18 18
 # Test Repository home
19 19
 my $rep_home = $ENV{GITPREP_REP_HOME} = "$FindBin::Bin/import_rep_user";
20 20
 
21
+$ENV{GITPREP_NO_MYCONFIG} = 1;
22
+
23
+
21 24
 use Gitprep;
22 25
 
23 26
 # For perl 5.8
+75
xt/need_login_always.t
... ...
@@ -0,0 +1,75 @@
1
+use Test::More 'no_plan';
2
+use strict;
3
+use warnings;
4
+
5
+use FindBin;
6
+use utf8;
7
+use lib "$FindBin::Bin/../mojo/lib";
8
+use lib "$FindBin::Bin/../lib";
9
+use lib "$FindBin::Bin/../extlib/lib/perl5";
10
+use File::Path 'rmtree';
11
+use Encode qw/encode decode/;
12
+
13
+use Test::Mojo;
14
+
15
+$ENV{GITPREP_TEST} = 1;
16
+
17
+# Test DB
18
+my $db_file = $ENV{GITPREP_DB_FILE} = "$FindBin::Bin/user.db";
19
+
20
+# Test Repository home
21
+my $rep_home = $ENV{GITPREP_REP_HOME} = "$FindBin::Bin/user";
22
+
23
+$ENV{GITPREP_NO_MYCONFIG} = 1;
24
+
25
+
26
+use Gitprep;
27
+
28
+# For perl 5.8
29
+{
30
+  no warnings 'redefine';
31
+  sub note { print STDERR "# $_[0]\n" unless $ENV{HARNESS_ACTIVE} }
32
+}
33
+
34
+note 'Start page';
35
+{
36
+  unlink $db_file;
37
+  rmtree $rep_home;
38
+  
39
+  my $app = Gitprep->new;
40
+  my $t = Test::Mojo->new($app);
41
+  $t->ua->max_redirects(3);
42
+  
43
+  $app->config->{basic}{need_login_always} = 1;
44
+  $app->config->{basic}{reset_password} = 1;
45
+  
46
+  # Access start page
47
+  $t->get_ok('/_start');
48
+  $t->content_like(qr/Start page/);
49
+
50
+  # Create admin user
51
+  $t->post_ok('/_start?op=create', form => {password => 'a', password2 => 'a'});
52
+  $t->content_like(qr/Login page/);
53
+
54
+  # Login as admin
55
+  $t->post_ok('/_login?op=login', form => {id => 'admin', password => 'a'});
56
+
57
+  # Create user
58
+  $t->post_ok('/_admin/user/create?op=create', form => {id => 'kimoto1', password => 'a', password2 => 'a'});
59
+  $t->content_like(qr/kimoto1/);
60
+
61
+  # Access reset password page
62
+  $t->get_ok('/reset-password');
63
+  $t->content_like(qr/Reset password page/);
64
+  
65
+  # Access login page
66
+  $t->get_ok('/_login');
67
+  $t->content_like(qr/Login page/);
68
+  
69
+  # Logout
70
+  $t->get_ok('/_logout');
71
+  
72
+  # Redirect to login page from other page
73
+  $t->get_ok('/_new');
74
+  $t->content_like(qr/Login page/);
75
+}
+8 -5
xt/user.t
... ...
@@ -20,6 +20,9 @@ my $db_file = $ENV{GITPREP_DB_FILE} = "$FindBin::Bin/user.db";
20 20
 # Test Repository home
21 21
 my $rep_home = $ENV{GITPREP_REP_HOME} = "$FindBin::Bin/user";
22 22
 
23
+$ENV{GITPREP_NO_MYCONFIG} = 1;
24
+
25
+
23 26
 use Gitprep;
24 27
 
25 28
 # For perl 5.8
... ...
@@ -62,7 +65,7 @@ note 'Start page';
62 65
   
63 66
   # Create admin user
64 67
   $t->post_ok('/_start?op=create', form => {password => 'a', password2 => 'a'});
65
-  $t->content_like(qr/Login Page/);
68
+  $t->content_like(qr/Login page/);
66 69
 
67 70
   # Admin user already exists(Redirect to top page)
68 71
   $t->post_ok('/_start?op=create', form => {password => 'a', password2 => 'a'});
... ...
@@ -79,11 +82,11 @@ note 'Admin pages';
79 82
 
80 83
   # Create admin user
81 84
   $t->post_ok('/_start?op=create', form => {password => 'a', password2 => 'a'});
82
-  $t->content_like(qr/Login Page/);
85
+  $t->content_like(qr/Login page/);
83 86
   
84 87
   # Page access
85 88
   $t->get_ok('/_login');
86
-  $t->content_like(qr/Login Page/);
89
+  $t->content_like(qr/Login page/);
87 90
   
88 91
   # Login fail
89 92
   $t->post_ok('/_login?op=login', form => {id => 'admin', password => 'b'});
... ...
@@ -212,7 +215,7 @@ note 'Reset password';
212 215
 
213 216
   # Create admin user
214 217
   $t->post_ok('/_start?op=create', form => {password => 'a', password2 => 'a'});
215
-  $t->content_like(qr/Login Page/);;
218
+  $t->content_like(qr/Login page/);;
216 219
 
217 220
   # Not loing user can't access
218 221
   $t->get_ok('/reset-password');
... ...
@@ -271,7 +274,7 @@ note 'Profile';
271 274
 
272 275
   # Create admin user
273 276
   $t->post_ok('/_start?op=create', form => {password => 'a', password2 => 'a'});
274
-  $t->content_like(qr/Login Page/);
277
+  $t->content_like(qr/Login page/);
275 278
 
276 279
   # Login as admin
277 280
   $t->post_ok('/_login?op=login', form => {id => 'admin', password => 'a'});