packageSmokeMachine::Plugin::Access;useMojo::Base'Mojolicious::Plugin';subaccess_slash{# Test if the header X-AUTH-KEY is a valid key# and add a User DBIC obj on $c->stash->{user}}subaccess_api{# Test if the header X-API-KEY is a valid key# and add a Client DBIC obj on $c->stash->{client}}subaccess_admin{# Test if the user has a admin role}subregister{my($self,$app)=@_;$app->helper("access_slash"=>\&access_slash);$app->helper("access_api"=>\&access_api);$app->helper("access_admin"=>\&access_admin);$app->helper("access"=>sub{my($c,@args)=@_;my$name=@args?$args[0]:$c->current_route;my%access=(admin_get_user=>"access_admin",get_obj=>"access_slash",);my$method=$access->{$name};return$c->$method();});}1;
packageSmokeMachine;useMojo::Base'Mojolicious';subdata{my$c=shift;my$route_name=shift;if("get_obj"){return$c->stash->{user}->find_related($c->stash->{obj_id})->data;}}# This method will run once at server startsubstartup{my$self=shift;$self->plugin('SmokeMachine::Plugin::Access');# Routermy$r=$self->routes;my$auth=$r->under(sub{my$c=shift;return$c->access($c);});$self->helper(data=>\&data);###$auth->get('/'=>sub{my$c=shift;});$auth->get('/object/:obj_id'=>sub{my$c=shift;$c->render(json=>$c->data($c->current_route);}=>"get_obj");###$auth->get('/admin/user/:user_id'=>sub{my$c=shift;$c->render(json=>$c->data($c->current_route);}=>"admin_get_user");###$auth->get('/api/client_data'=>sub{my$c=shift;$c->render(json=>$c->data($c->current_route);}=>"client_data");###$r->get('/ping'=>sub{shift()->render(json=>"pong");}=>"ping");###$r->websocket('/ws'=>sub{my$c=shift;$c->on(json=>sub{# example: {# "route": "admin_get_user",# "headers": {# "X-API-KEY": "1234567890123456789012345678901234567890",# "X-AUTH-KEY": "1234567890123456789012345678901234567890"# },# "stash": {# "user_id": 42# }#}my$tx=shift;my$msg=shift;# PseudoCodeeval{dieunless$c->access($msg->{route});};if($@){warn("No access");}else{my$hash=$c->data($msg->{route});}});});}1;
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.