Skip to content

Instantly share code, notes, and snippets.

@mathias-goebel
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathias-goebel/78b3db9119e41d6a75b2 to your computer and use it in GitHub Desktop.
Save mathias-goebel/78b3db9119e41d6a75b2 to your computer and use it in GitHub Desktop.
tg-sade-usr-mgmt
<!--
to be placed in /[PROJECTS]/[PROJECT-NAME]/templates/tmpl1/
requires: module and sign-up.html
-->
<div data-template="templates:init">
<div data-template="templates:surround" data-template-with="page_index.html" data-template-at="content-container">
<div data-template="usr:new"/>
</div>
</div>
<div data-template="templates:init">
<div data-template="templates:surround" data-template-with="page_index.html" data-template-at="content-container">
<!-- Main body
================== -->
<div class="wrapper">
<!-- Page Tip -->
<div class="page-tip animated slideInDown">
<div class="container">
<div class="row">
<div class="col-sm-12">
<p>Fill out the form below to sign up.</p>
</div>
</div>
</div>
</div>
<!-- Main Form -->
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 signup">
<h3 class="hl top-zero">Registration</h3>
<hr/>
<form role="form" action="00test.html" method="post">
<div class="form-group">
<input type="text" class="form-control" id="projectname" placeholder="Projektname" data-toggle="popover" data-placement="left" data-trigger="focus" data-content="Erlaubt sind nur lateinische Buchstaben und Zahlen sowie &#34;-&#34; und &#34;_&#34;. Leerzeichen werden automatisch durch &#34;_&#34; ersetzt." data-original-title="Projektname" name="project-name"/>
</div>
<div class="form-group">
<input type="text" class="form-control" id="newuser" placeholder="Ihr Name" data-toggle="popover" data-placement="left" data-trigger="focus" data-content="Erlaubt sind nur lateinische Buchstaben und Zahlen sowie &#34;-&#34; und &#34;_&#34;. Leerzeichen werden automatisch durch &#34;_&#34; ersetzt." data-original-title="Ihr Name" name="newuser"/>
</div>
<div class="form-group">
<input type="email" class="form-control" id="InEmail" placeholder="Enter email" data-toggle="popover" data-placement="left" data-trigger="focus" data-content="Enter a valid email here." data-original-title="Email" name="email"/>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<input type="password" class="form-control margin-bottom-xs" id="Password1" placeholder="Password" data-toggle="popover" data-placement="left" data-trigger="focus" data-content="Complexity of 41% at least!" data-original-title="Password" name="dwp"/>
</div>
<div class="col-sm-5">
<div id="progressbar">
<div id="progress"/>
</div>
</div>
<div class="col-sm-1">
<div id="status">
<div id="complexity"/>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<input type="password" class="form-control" id="Password2" placeholder="Repeat Password" data-toggle="popover" data-trigger="focus" data-content="Make sure you still remember it." data-original-title="Repeat Password" name="pwd"/>
</div>
<div class="col-sm-5">
<button type="button" class="btn btn-default" id="cpw">Generate secure password</button>
</div>
<div class="col-sm-1"/>
</div>
</div>
<div class="checkbox">
<label>
<input name="tospp" type="checkbox"/> I agree to the <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>
</label>
</div>
<input type="submit" class="btn btn-warning btn-disabled" disabled="disabled"/>
</form>
</div>
</div>
</div>
<link rel="stylesheet" href="css/complex.css"/>
<script type="text/javascript">
$( document ).ready(function() {
$("#Password1").complexify({}, function (valid, complexity) {
if (!valid) {
$('#progress').css({'width':complexity + '%'}).removeClass('progressbarValid').addClass('progressbarInvalid');
$('.btn-green').addClass('btn-warning').removeClass('btn-green').prop({disabled: true});
} else {
$('#progress').css({'width':complexity + '%'}).removeClass('progressbarInvalid').addClass('progressbarValid');
$('.btn-warning').addClass('btn-green').removeClass('btn-warning').prop({disabled: false});
}
$('#complexity').html(Math.round(complexity) + '%');
});
$('#cpw').click(function() {
var text = $.password(18);
$( "#Password1" ).val( text );
$( "#Password2" ).val( text );
$( "#Password1" ).focus();
});
});
</script>
<script src="js/pwcreator.js"/>
</div>
</div>
</div>
xquery version "3.0";
module namespace usr = "http://sade/usrmgmt" ;
declare namespace templates="http://exist-db.org/xquery/templates";
import module namespace config="http://exist-db.org/xquery/apps/config" at "../../core/config.xqm";
import module namespace app="http://sade/app" at "../../core/app.xql";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
declare function local:chown-docs-recursive($collection, $usr) {
for $resource in xmldb:get-child-resources($collection)
return
sm:chown(xs:anyURI($collection || '/' || $resource), $usr),
for $child in xmldb:get-child-collections($collection)
return
local:chown-docs-recursive(concat($collection, "/", $child), $usr)
};
declare function local:chown-colls-recursive($collection, $usr) {
for $coll in xmldb:get-child-collections($collection)
return
sm:chown(xs:anyURI($collection || '/' || $coll), $usr),
for $child in xmldb:get-child-collections($collection)
return
local:chown-colls-recursive(concat($collection, "/", $child), $usr)
};
declare %templates:wrap function usr:create($usr, $pwd, $project-name) {
let $login:= xmldb:login('/', 'admin', 'TextGrid#?SADE'),
$egal := sm:create-account($usr, $pwd, ''),
$egal := xmldb:copy('/db/sade-projects/textgrid/', '/db/sade-projects/tmp'),
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='project-id'] with <param key="project-id">{$project-name}</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='project-title'] with <param key="project-title">{$project-name}</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='sade.user'] with <param key="sade.user">{$usr}</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='sade.password'] with <param key="sade.password">{$pwd}</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='textgrid.user'] with <param key="textgrid.user">TODO</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='textgrid.password'] with <param key="textgrid.password">TODO</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='textgrid.sidcachepath'] with <param key="textgrid.sidcachepath">{'/sade-projects/' || $project-name || '/'}</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='textgrid.sidcachepath.user'] with<param key="textgrid.sidcachepath.user">{$usr}</param>,
$egal := update replace doc('/db/sade-projects/tmp/textgrid/config.xml')//param[@key ='textgrid.sidcachepath.password'] with<param key="textgrid.sidcachepath.password">{$pwd}</param>,
$egal := xmldb:rename('/db/sade-projects/tmp/textgrid', $project-name),
$egal := xmldb:move('/db/sade-projects/tmp/'|| $project-name, '/db/sade-projects/'),
(: Remove sample data :)
$egal := xmldb:remove('/db/sade-projects/'|| $project-name ||'/data/xml/data'),
$egal := xmldb:remove('/db/sade-projects/'|| $project-name ||'/data/xml/meta'),
$egal := xmldb:remove('/db/sade-projects/'|| $project-name ||'/data/xml/tile'),
$egal := xmldb:remove('/db/sade-projects/'|| $project-name ||'/data/xml/agg'),
$egal := xmldb:create-collection('/db/sade-projects/'|| $project-name ||'/data/xml', 'data'),
$egal := xmldb:create-collection('/db/sade-projects/'|| $project-name ||'/data/xml', 'meta'),
$egal := xmldb:create-collection('/db/sade-projects/'|| $project-name ||'/data/xml', 'tile'),
$egal := xmldb:create-collection('/db/sade-projects/'|| $project-name ||'/data/xml', 'agg'),
$nav1 := (<navigation>
<!-- This file is responsible for the navigation bar on top of the page.
It will be update automatically if one submits data from the TextGrid Laboratory.
THIS MAY OVERRIDES YOUR COSTUMIZATIONS! -->
<submenu label="Dokumentation">
<item label="Anpassen des Portals" link="index.html?id=webgui.md"/>
<item label="Facetierte Suche" link="index.html?id=fsearch.md"/>
<item label="Publizieren aus TextGridLab" link="index.html?id=sadepublish.md"/>
<item label="TODO" link="index.html?id=TODO.md"/>
</submenu>
<submenu label="Weiterführende Links" link="#">
<item label="eXist Dashboard" link="/exist/apps/dashboard/index.html" target="_blank"/>
<item label="eXist Dokumentation" link="http://exist-db.org/exist/apps/doc/"/>
<item label="eXist XQuery Function Dokumentation" link="http://exist-db.org/exist/apps/fundocs/index.html"/>
<divider/>
<item label="XQuery Wikibook" link="http://en.wikibooks.org/wiki/XQuery"/>
<item label="TextGrid Benutzerhandbuch" link="https://dev2.dariah.eu/wiki/display/TextGrid/User+Manual+2.0"/>
<item label="TextGrid Homepage" link="http://textgrid.de"/>
</submenu>
<item label="Info" link="info.html"/>
</navigation>),
$egal := xmldb:store('/db/sade-projects/'|| $project-name ||'/', 'navigation.xml', $nav1 , 'text/xml'),
$nav2 := <div class="row">
<div class="span3">
<div class="well">
<div id="nav-textgrid">
<ul class="nav nav-list">
<li>
<label class="tree-toggle nav-header">
<a href="index.html">Home</a>
</label>
</li>
</ul>
</div>
</div>
</div>
</div>,
$egal := xmldb:store('/db/sade-projects/'|| $project-name ||'/', 'navigation-bootstrap3.xml', $nav2 , 'text/xml'),
$egal := xmldb:store('/db/sade-projects/'|| $project-name ||'/', 'navigation-tmpl1.xml', $nav2 , 'text/xml'),
(: done :)
$egal := sm:chmod(xs:anyURI('/db/sade-projects/' || $project-name), 'rwxr-xr-x'),
$egal := sm:chmod(xs:anyURI('/db/sade-projects/' || $project-name || "/config.xml"), 'rw----r-x'),
$egal := xmldb:remove(xs:anyURI('/db/sade-projects/' || $project-name), "sid.xml"),
$egal := sm:chmod(xs:anyURI('/db/sade-projects/' || $project-name || '/templates/tmpl1/deleteall.html'), 'rwx------'),
$egal := sm:chmod(xs:anyURI('/db/sade-projects/' || $project-name || '/templates/tmpl1/info.html'), 'rwx------'),
$egal := sm:chown(xs:anyURI('/db/sade-projects/' || $project-name), $usr),
$egal := local:chown-docs-recursive('/db/sade-projects/' || $project-name, $usr),
$egal := local:chown-colls-recursive('/db/sade-projects/' || $project-name, $usr)
return 'ok'
};
declare %templates:wrap function usr:mail($usr, $pwd, $project-name) {
let $message :=
<mail>
<from>sade-register@c100-153.cloud.gwdg.de</from>
<reply-to>support@textgrid.de</reply-to>
<to>{request:get-parameter('email', '')}</to>
<cc/>
<bcc/>
<subject>Your SADE instance</subject>
<message>
<text>
You have successfully acquired a new SADE instance and you can now start configuring and personalizing.
Here are the needed links:
- http://141.5.100.153/exist/apps/SADE/{$project-name}/index.html
- http://141.5.100.153/exist/apps/eXide/index.html?open=/db/sade-projects/{$project-name}/config.xml
You might want to send data from the TextGridLab. Please give the SADE Publish Tool (TextGridLab Plug-in) the following points:
URL: http://141.5.100.153/exist/apps/textgrid-connect/publish/{$project-name}
User: {$usr}
Password: {$pwd}
</text>
<xhtml>
<p>You have successfully acquired a new SADE instance and you can now start configuring and personalizing.<br/>
<a href="http://141.5.100.153/exist/apps/SADE/{$project-name}/index.html">View your instance</a><br/>
<a href="http://141.5.100.153/exist/apps/eXide/index.html?open=/db/sade-projects/{$project-name}/config.xml">Configuration</a><br/>
You might want to send data from the TextGridLab. Please give the SADE Publish Tool (TextGridLab Plug-in) the following points:
<ul><li>URL: http://141.5.100.153/exist/apps/textgrid-connect/publish/{$project-name}</li>
<li>User: {$usr}</li>
<li>Password: {$pwd}</li>
</ul>
</p>
</xhtml>
</message>
</mail>
let $mail-handle := mail:get-mail-session
(
<properties>
<property name="mail.smtp.host" value="localhost"/>
<property name="mail.smtp.port" value="25"/>
<property name="mail.smtp.auth" value="false"/>
<property name="mail.smtp.user" value=""/>
<property name="mail.smtp.password" value=""/>
<property name="mail.smtp.allow8bitmime" value="true"/>
</properties>
),
$egal := mail:send-email($mail-handle, $message)
return "ok"
};
declare %templates:wrap function usr:new($node as node(), $model as map(*)) {
let $login := xmldb:login('/', 'admin', 'TODO')
let $usr := (replace(request:get-parameter('newuser', ''), '\s', '_')),
$pwd := request:get-parameter('pwd', ''),
$project-name := (replace(request:get-parameter('project-name', ''), '\s', '_'))
let $test1 := if((request:get-parameter('pwd', '') = request:get-parameter('dwp', '')))
then 'ok'
else 'Passwords dont match or not complex.',
$test2 := if(sm:user-exists($usr)) then $usr|| " already registered. Please choose a different name." else "ok",
$test3 := if(xmldb:collection-available('/db/sade-projects/' || $project-name)) then "This project name is already in use, please choose a different one." else "ok",
$test4 := if(matches(concat($usr, $pwd, $project-name), '[^A-Za-z0-9_-]')) then "It seems that you send a least one special character. Don't do it again." else "ok"
return
if ($test1||$test2||$test3||$test4 = "okokokok") then
<div class="row">
<p>creating account... {usr:create($usr, $pwd, $project-name)}</p>
<p>sending mail... {usr:mail($usr, $pwd, $project-name)}</p>
<p>preparing range index for facetted search... {usr:indexconfig($project-name)}</p>
<p>In a few seconds (hopefully) you receive an E-Mail containing all neccessary data to start configuring and personlizing your own instance.</p>
</div>
else <div class="row">
<h1>Oh! Something went wrong!</h1>
<p>Please fullfill the requirements of the form.</p>
<p>Special characters: {$test4}</p>
<p>Project: {$test3}</p>
<p>User: {$test2}</p>
<p>Password: {$test1}</p>
</div>
};
declare function usr:indexconfig($project-name) {
xmldb:copy('/db/system/config/db/sade-projects/textgrid/data', xmldb:create-collection('/db/system/config/db/sade-projects', $project-name))
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment