Browse Source

routes, some fixes

themage
Marco Neves 7 years ago
parent
commit
0cd00ee903

+ 0 - 8
lib/oSMo/Auth.pm

@ -5,10 +5,6 @@ use oSMo::Base qw(Mojolicious::Controller);
5 5
has auth_forms	=> sub { {} };
6 6
has user_checks	=> sub { {} };
7 7
8
has	common_routes			=> undef;
9
has anonymous_routes	=> undef;
10
has	authed_routes			=> undef;
11
12 8
has app								=> undef;
13 9
14 10
my @userinit_fields = (
@ -54,10 +50,6 @@ sub activate {
54 50
	$app->routes->add_condition( authed_user => \&authed_condition );
55 51
	$app->routes->add_condition( anonym_user => \&anonym_condition );
56 52
	$app->routes->add_condition( user_can		 => \&user_can_condition );
57
58
	$self->common_routes( $app->routes->route('/') );
59
	$self->anonymous_routes( $app->routes->route('/')->over('anonym_user') );
60
	$self->authed_routes( $app->routes->route('/')->over('authed_user') );
61 53
}
62 54
63 55
sub auth_handler {

+ 1 - 5
lib/oSMo/Dashboards.pm

@ -37,11 +37,7 @@ sub add_dashboard {
37 37
38 38
	my $route = $opts{'route'} || "/$dashboard/";
39 39
	my @perms = $opts{'permission'}||@{ $opts{'permissions'} || [] };
40
	my $routes = @perms
41
		? $self->app->auth->authed_routes
42
		: $self->app->auth
43
			? $self->app->auth->common_routes
44
			: $self->app->routes;
40
	my $routes = $self->app->routes;
45 41
46 42
	my $cb = sub {
47 43
		$self->_handle_dashboard( $dashboard => @_ );

+ 1 - 1
lib/oSMo/Plugin/Auth/Email.pm

@ -68,7 +68,7 @@ sub register {
68 68
		);
69 69
	$osmo->auth->add_user_check('email' => sub { $self->check_user(@_) });
70 70
71
	my $ar = $osmo->auth->anonymous_routes;
71
	my $ar = $osmo->routes->over('anonym_user');
72 72
	$ar->route('/register/email/')
73 73
		->to(cb => sub { $self->handle_register(@_) });
74 74

+ 1 - 1
lib/oSMo/Routes.pm

@ -6,7 +6,7 @@ use oSMo::Routes::Route;
6 6
7 7
*find_route = *oSMo::Routes::Route::find_route;
8 8
*add_child  = *oSMo::Routes::Route::add_child;
9
*__route_key  = *oSMo::Routes::Route::__route_key;
9
*over  			= *oSMo::Routes::Route::over;
10 10
11 11
has route_table => sub { {} };
12 12

+ 13 - 5
lib/oSMo/Routes/Route.pm

@ -14,6 +14,7 @@ sub route {
14 14
15 15
	unless ( $existing ) {
16 16
		$self->add_child( $route );
17
17 18
		$existing = $route;
18 19
	}
19 20
@ -35,14 +36,14 @@ sub over {
35 36
	return $self->SUPER::over()
36 37
		unless @_;
37 38
38
	my $rt = $self->_clone;
39
	my $rt = _clone($self);
39 40
	$rt->SUPER::over( @_ );
40 41
41 42
	if ($self->parent and my $r = $self->parent->find_route( $rt )) {
42 43
		return $r;
43 44
	}
44 45
45
	unless ($self->over or @{$self->children}) {
46
	unless ($self->over or @{$self->children} or !$self->parent) {
46 47
		my $ok = __route_key($self);
47 48
		$self->SUPER::over(@_);
48 49
		if ($self->parent) {
@ -56,6 +57,7 @@ sub over {
56 57
	my $route = $self->find_route( $rt );
57 58
	unless ($route) {
58 59
		$self->add_child( $rt );
60
59 61
		$route = $rt;
60 62
	}
61 63
@ -67,15 +69,21 @@ sub find_route {
67 69
68 70
	my $k = __route_key( $route );
69 71
72
	print STDERR "Looking for '$k' => ",$self->route_table->{$k}?'Found':'Not Found',"\n\t == FROM: ", join(':',(caller())[1,2]),"\n";
70 73
	return $self->route_table->{$k};
71 74
}
72 75
73 76
sub _clone {
74 77
	my $self = shift;
75 78
76
	my $nroute = $self->new();
77
	$nroute->parent( 	$self->parent  );
78
	$nroute->pattern( $self->pattern );
79
	my $nroute = oSMo::Routes::Route->new();
80
	$nroute->parent( 	$self->parent||$self  );
81
	$nroute->pattern(
82
			$self->pattern->new(
83
				$self->pattern->pattern,
84
				%{$self->pattern->constraints},
85
			)
86
		);
79 87
80 88
	return $nroute;
81 89
}

+ 50 - 3
templates/errors/not_found.development.html.ep

@ -13,12 +13,54 @@
13 13
					<code><%== '&nbsp;' x $depth %><%= $pattern %></code>
14 14
				</td>
15 15
				<td>
16
					% my $constraints = $route->pattern->constraints || {};
17
					% for my $k (sort keys %$constraints) {
18
					% 	my $val = ref $constraints->{$k} eq 'ARRAY'
19
					%			? '[qw('.join(' ',@{ $constraints->{$k} }).')]'
20
					%			: $constraints->{$k};
21
					<code> <%=$k%> => <%= $val %></code>
22
					% }
23
				</td>
24
				<td>
16 25
					<code><%= uc(join ',', @{$route->via || []}) || '*' %></code>
17 26
				</td>
18 27
				<td>
19
					% my $name = $route->name;
20
					<code><%= $route->has_custom_name ? qq{"$name"} : $name %></code>
28
					% my $cond = $route->over;
29
					% if ($cond) {
30
					% $cond = [ @{$cond} ]; #copy it
31
					% while (@$cond) {
32
					%		my $name = shift @{$cond};
33
					%   my $args = shift @{$cond};
34
					%   $args = '["'.join('","', @$args).'"]' if ref $args and @$args;
35
					<code><%= $name  %>(<%=$args%>)</code>
36
					%	}
37
					% }
38
				</td>
39
% if (param('extended')) {
40
				<td>
41
					% my $defaults = $route->pattern->defaults;
42
					% if ($defaults->{cb}) {
43
					<code>cb->()</code>
44
					% } elsif ($defaults->{controller}) {
45
						<code>
46
						<%= $defaults->{controller} %>#<%= $defaults->{action}||'_'%>
47
						</code>
48
					% } elsif ($defaults->{namespace}) {
49
						<code>
50
						<%= $defaults->{namespace} %>-><%=$defaults->{action}||'_'%>()
51
						</code>
52
					% } else {
53
					%		for my $k (keys %$defaults) {
54
						<code>
55
						<%= $k %> => <%= $defaults->{$k} // 'undef' %>
56
						</code>
57
					% 	}
58
					% }
59
				</td>
60
				<td>
61
					<code><%=$route->__route_key%></code>
21 62
				</td>
63
% } #extended
22 64
			</tr>
23 65
			% $depth++;
24 66
			%= $walk->($walk, $_, $depth) for @{$route->children};
@ -27,8 +69,13 @@
27 69
		<table class="table table-condensed table-striped table-hover">
28 70
			<tr>
29 71
				<th>Pattern</th>
72
				<th>Constraints</th>
30 73
				<th>Methods</th>
31
				<th>Name</th>
74
				<th>Conditions</th>
75
% if (param('extended')) {
76
				<th>To</th>
77
				<th>Key</th>
78
% }
32 79
			</tr>
33 80
			%= $walk->($walk, $_, 0) for @{app->routes->children};
34 81
		</table>