Skip to content

Commit

Permalink
Merge pull request #340 from eXist-db/mysec
Browse files Browse the repository at this point in the history
add secure area option to generator
  • Loading branch information
duncdrum authored Feb 2, 2020
2 parents fe6604d + d72979f commit 72aaf58
Show file tree
Hide file tree
Showing 24 changed files with 359 additions and 21 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ If you want to contribute another template or option, please take a look at the

MIT © [Duncan Paterson](https://github.com/duncdrum)


[npm-image]: https://badge.fury.io/js/%40existdb%2Fgenerator-exist.svg
[npm-url]: https://www.npmjs.com/package/@existdb/generator-exist
[travis-image]: https://travis-ci.com/eXist-db/generator-exist.svg?token=qpLmm7SAUYJsXY8vZsRs&branch=master
Expand Down
29 changes: 25 additions & 4 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ module.exports = class extends Generator {
// message: 'How would you like to build your app?',
// default: 'ant'
// },
{
when: response => {
return response.apptype[1] === 'application'
},
type: 'confirm',
name: 'mysec',
message: 'should your app have a secure area?',
default: false,
store: true
},

// Path related
{
Expand Down Expand Up @@ -498,6 +508,13 @@ module.exports = class extends Generator {
apptype: this.props.apptype[0]
})
}
// secure area (mysec)
if (this.props.mysec) {
this.fs.copy(
this.templatePath('mysec/**'),
this.destinationPath('')
)
}
// distinct contents (flexible)
switch (this.props.apptype[0]) {
case 'exist-design':
Expand Down Expand Up @@ -549,7 +566,8 @@ module.exports = class extends Generator {
this.fs.copyTpl(
this.templatePath('controller.xql'),
this.destinationPath('controller.xql'), {
apptype: this.props.apptype[0]
apptype: this.props.apptype[0],
mysec: this.props.mysec
})

this.fs.copyTpl(
Expand All @@ -571,7 +589,8 @@ module.exports = class extends Generator {
version: this.props.version,
author: this.props.author,
website: this.props.website,
title: this.props.title
title: this.props.title,
mysec: this.props.mysec
})
this.fs.copyTpl(
this.templatePath('config.xqm'),
Expand Down Expand Up @@ -616,14 +635,16 @@ module.exports = class extends Generator {
this.fs.copyTpl(
this.templatePath('exist-design/page.html'),
this.destinationPath('templates/page.html'), {
title: this.props.title
title: this.props.title,
mysec: this.props.mysec
})
break
case 'plain':
this.fs.copyTpl(
this.templatePath('exist-plain/page.html'),
this.destinationPath('templates/page.html'), {
title: this.props.title
title: this.props.title,
mysec: this.props.mysec
})
break
default:
Expand Down
42 changes: 42 additions & 0 deletions generators/app/templates/app.xql
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,45 @@ declare
function app:foo($node as node(), $model as map(*)) {
<p>Dummy templating function.</p>
};
<%_ if (mysec) { %>
declare function app:test($node as node(), $model as map(*)) {
<p>Dummy template output generated by function app:test at {current-dateTime()}. The templating
function was triggered by the class attribute <code>class="app:test"</code>.</p>
};

declare function app:if-attribute-set($node as node(), $model as map(*), $attribute as xs:string) {
let $isSet :=
(exists($attribute) and request:get-attribute($attribute))
return
if ($isSet) then
templates:process($node/node(), $model)
else
()
};

declare function app:if-attribute-unset($node as node(), $model as map(*), $attribute as xs:string) {
let $isSet :=
(exists($attribute) and request:get-attribute($attribute))
return
if (not($isSet)) then
templates:process($node/node(), $model)
else
()
};

declare function app:username($node as node(), $model as map(*)) {
let $user:= request:get-attribute("org.exist-db.mysec.user")
let $name := if ($user) then sm:get-account-metadata($user, xs:anyURI('http://axschema.org/namePerson')) else 'Guest'
return if ($name) then $name else $user
};

declare
%templates:wrap
function app:userinfo($node as node(), $model as map(*)) as map(*) {
let $user:= request:get-attribute("org.exist-db.mysec.user")
let $name := if ($user) then sm:get-account-metadata($user, xs:anyURI('http://axschema.org/namePerson')) else 'Guest'
let $group := if ($user) then sm:get-user-groups($user) else 'guest'
return
map { "user-id" : $user, "user-name" : $name, "user-groups" : $group}
};
<% } %>
5 changes: 5 additions & 0 deletions generators/app/templates/config.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ xquery version "3.1";
: within a module.
:)

module namespace config="<%- defuri %>/<%- defcoll %>/<%- short %>/config";

declare namespace templates="http://exist-db.org/xquery/templates";
declare namespace repo="http://exist-db.org/xquery/repo";
declare namespace expath="http://expath.org/ns/pkg";
(:
Determine the application root collection from the current module load path.
:)
Expand Down
15 changes: 14 additions & 1 deletion generators/app/templates/controller.xql
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@ xquery version "3.1";
declare namespace control = "http://exist-db.org/apps/dashboard/controller";
declare namespace output = "http://exquery.org/ns/rest/annotation/output";
declare namespace rest = "http://exquery.org/ns/restxq";
<% } %>
<% } -%>

<%_ if (mysec) { %>
import module namespace login="http://exist-db.org/xquery/login" at "resource:org/exist/xquery/modules/persistentlogin/login.xql";
<% } -%>

declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $exist:prefix external;
declare variable $exist:root external;

<%_ if (mysec) { %>
declare variable $local:login_domain := "org.exist-db.mysec";
declare variable $local:user := $local:login_domain || '.user';

let $logout := request:get-parameter("logout", ())
let $set-user := login:set-user($local:login_domain, (), false())
return
<% } %>

if ($exist:path eq '') then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{request:get-uri()}/"/>
Expand Down
23 changes: 23 additions & 0 deletions generators/app/templates/exist-design/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
</ul>
</li>
</ul>
<%_ if (mysec) { %>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="../">Hello <span data-template="app:username"/>
</a>
</li>
<li>
<div data-template="app:if-attribute-set" data-template-attribute="org.exist-db.mysec.user">
<a href="index.html?logout=true" class="btn btn-default">×</a>
</div>
<div data-template="app:if-attribute-unset" data-template-attribute="org.exist-db.mysec.user">
<a data-toggle="modal" href="#loginModal" class="btn btn-default">Login</a>
</div>
</li>
<li>
</li>
</ul>
<% } -%>
</div>
</nav>
</div>
Expand All @@ -57,5 +75,10 @@
</div>
</div>
</div>
<%_ if (mysec) { %>
<div data-template="templates:if-parameter-unset" data-template-param="org.exist-db.mysec.user">
<div data-template="templates:include" data-template-path="templates/login-panel.html"/>
</div>
<% } -%>
</body>
</html>
27 changes: 25 additions & 2 deletions generators/app/templates/exist-plain/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<link rel="stylesheet" type="text/css" href="resources/css/style.css"/>
<script type="text/javascript" src="$shared/resources/scripts/jquery/jquery-1.7.1.min.js"/>
<script type="text/javascript" src="$shared/resources/scripts/bootstrap-3.0.3.min.js"/>
<script type="text/javascript" src="$shared/resources/scripts/loadsource.js"/>
<script type="text/javascript" src="$shared/resources/scripts/loadsource.js"/>
</head>
<body id="body">
<nav class="navbar navbar-default" role="navigation">
Expand All @@ -31,6 +31,24 @@
<a href="index.html">Home</a>
</li>
</ul>
<%_ if (mysec) { %>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="../">Hello <span data-template="app:username"/>
</a>
</li>
<li>
<div data-template="app:if-attribute-set" data-template-attribute="org.exist-db.mysec.user">
<a href="index.html?logout=true" class="btn btn-default">×</a>
</div>
<div data-template="app:if-attribute-unset" data-template-attribute="org.exist-db.mysec.user">
<a data-toggle="modal" href="#loginModal" class="btn btn-default">Login</a>
</div>
</li>
<li>
</li>
</ul>
<% } -%>
</li>
</ul>
</div>
Expand All @@ -41,5 +59,10 @@
<img src="$shared/resources/images/powered-by.svg" alt="Powered by eXist-db"/>
</a>
</footer>
<%_ if (mysec) { %>
<div data-template="templates:if-parameter-unset" data-template-param="org.exist-db.mysec.user">
<div data-template="templates:include" data-template-path="templates/login-panel.html"/>
</div>
<% } -%>
</body>
</html>
</html>
3 changes: 1 addition & 2 deletions generators/app/templates/github/contributing.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing

:balloon: First off, thank you for considering contributing to <%- title %>. :balloon:
:balloon: First off, thank you for considering contributing to <%- title %>. :balloon:

All pull-requests are welcome. File a bug report, fix a typo, improve the documentation, or add a new feature. All are helpful, and make <%- title %> better.

Expand All @@ -13,7 +13,6 @@ Please have a quick look at these guidelines to help both you and the developers

If this is your first time contributing to a project you might want to take a look [here](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github).


## Getting started
1. Create your own fork of the code.
2. Do the changes in your fork.
Expand Down
1 change: 0 additions & 1 deletion generators/app/templates/github/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ You can take a look at the [Contribution guidelines for this project](.github/CO
<%- license %> © [<%- author %>](<%- website %>)
[license-img]: https://img.shields.io/badge/license-<%- badge %>-blue.svg
[license-url]: <%- badgelink %>
[release-img]: https://img.shields.io/badge/release-<%- version %>-green.svg
Expand Down
82 changes: 82 additions & 0 deletions generators/app/templates/mysec/admin/controller.xql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
xquery version "3.0";

(:~ The controller library contains URL routing functions.
:
: @see http://www.exist-db.org/exist/apps/doc/urlrewrite.xml
:)

import module namespace login="http://exist-db.org/xquery/login" at "resource:org/exist/xquery/modules/persistentlogin/login.xql";

declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $exist:prefix external;
declare variable $exist:root external;

declare variable $local:login_domain := "org.exist-db.mysec";
declare variable $local:user := $local:login_domain || '.user';

let $logout := request:get-parameter("logout", ())
let $set-user := login:set-user($local:login_domain, (), false())
return
if ($exist:path eq '') then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{request:get-uri()}/"/>
</dispatch>
else if (($exist:path eq "/") or ($logout)) then
(: forward root path to index.xql :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="index.html"/>
</dispatch>

else if (ends-with($exist:resource, ".html")) then
(: the html page is run through view.xql to expand templates :)
if (request:get-attribute("org.exist-db.mysec.user")) then
(: secured area checks user status :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<view>
<forward url="{$exist:controller}/../modules/view.xql">
<set-attribute name="isAdmin" value="true"/>
<set-attribute name="$exist:prefix" value="{$exist:prefix}"/>
<set-attribute name="$exist:controller" value="{$exist:controller}"/>
</forward>
</view>
<error-handler>
<forward url="{$exist:controller}/../error-page.html" method="get"/>
<forward url="{$exist:controller}/../modules/view.xql"/>
</error-handler>
</dispatch>
else
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<!-- This forwards the entry to the content page blog.html -->
<forward url="{$exist:controller}/security.html"/>
<!-- This send the page through the templating process -->
<view>
<forward url="{$exist:controller}/../modules/view.xql">
<set-attribute name="$exist:prefix" value="{$exist:prefix}"/>
<set-attribute name="$exist:controller" value="{$exist:controller}"/>
</forward>
</view>
<error-handler>
<forward url="{$exist:controller}/../error-page.html" method="get"/>
<forward url="{$exist:controller}/../modules/view.xql"/>
</error-handler>
</dispatch>
(: Resource paths starting with $shared are loaded from the shared-resources app :)
else if (contains($exist:path, "/$shared/")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="/shared-resources/{substring-after($exist:path, '/$shared/')}">
<set-header name="Cache-Control" value="max-age=3600, must-revalidate"/>
</forward>
</dispatch>
else if (starts-with($exist:path, "/resources")) then
(: images, css are contained in the top /resources/ collection. :)
(: Relative path requests from sub-collections are redirected there :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/../{$exist:path}"/>
</dispatch>
else
(: everything else is passed through :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<cache-control cache="yes"/>
</dispatch>
17 changes: 17 additions & 0 deletions generators/app/templates/mysec/admin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<div xmlns="http://www.w3.org/1999/xhtml" data-template="templates:surround" data-template-with="templates/page.html" data-template-at="content">
<div class="row">
<div class="col-md-8">
<div class="page-header">
<h1 data-template="config:app-title">Generated page</h1>
</div>
<div class="alert alert-success">
<p>The page template uses the <a href="http://twitter.github.com/bootstrap/">Bootstrap</a> CSS library for the page layout.</p>
</div>
</div>
<div class="col-md-4">
<h2>Application Info</h2>
<div data-template="config:app-info"/>
</div>
</div>
</div>
18 changes: 18 additions & 0 deletions generators/app/templates/mysec/admin/security.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<div xmlns="http://www.w3.org/1999/xhtml" data-template="templates:surround" data-template-with="templates/page.html" data-template-at="content">
<div class="row">
<div class="col-md-8">
<div class="page-header">
<h1 data-template="config:app-title">Generated page</h1>
</div>
<div class="alert alert-danger">
<p>This is a protected page. You must be logged in as a user with the appropriate
privileges.</p>
</div>
</div>
<div class="col-md-4">
<h2>Application Info</h2>
<div data-template="config:app-info"/>
</div>
</div>
</div>
Loading

0 comments on commit 72aaf58

Please sign in to comment.