8000 new example · nCodefresh/examples@ab90c50 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit ab90c50

Browse files
committed
new example
1 parent 56d709e commit ab90c50

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
exports.install = function() {
2+
framework.route('/');
3+
4+
framework.route('/', view_administrator, ['authorize', '@administrator']);
5+
framework.route('/', view_moderator, ['authorize', '@moderator']);
6+
7+
framework.route('/login/', redirect_login, ['unauthorize']);
8+
framework.route('/logoff/', redirect_logoff);
9+
};
10+
11+
function view_administrator() {
12+
var self = this;
13+
self.view('administrator');
14+
}
15+
16+
function view_moderator() {
17+
var self = this;
18+
self.view('moderator');
19+
}
20+
21+
function redirect_login() {
22+
var self = this;
23+
switch (self.query.user) {
24+
case 'administrator':
25+
case 'moderator':
26+
self.res.cookie('__user', self.query.user, '1 day');
27+
self.redirect('/');
28+
break;
29+
default:
30+
self.throw401();
31+
return;
32+
}
33+
}
34+
35+
function redirect_logoff() {
36+
var self = this;
37+
self.res.cookie('__user', '', new Date().add('d', -1));
38+
self.redirect('/');
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ================================================
2+
// AUTHORIZATION
3+
// ================================================
4+
5+
framework.onAuthorization = function(req, res, flags, next) {
6+
7+
var self = this;
8+
var cookie = req.cookie('__user');
9+
10+
switch (cookie) {
11+
case 'administrator':
12+
case 'moderator':
13+
flags.push('@' + cookie);
14+
next(true, { name: cookie });
15+
return;
16+
}
17+
18+
next(false);
19+
};

authorization-roles/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ===================================================
2+
// IMPORTANT: only for development
3+
// total.js - web application framework for node.js
4+
// http://www.totaljs.com
5+
// ===================================================
6+
7+
require('total.js').< 10000 span class=pl-en>http('debug');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>administrator.html: @{user.name}</h1>
2+
<div><a href="/logoff/">Sign out</a></div>

authorization-roles/views/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<div><a href="/login/?user=administrator">Sign in as Administrator</a></div>
2+
<div><a href="/login/?user=moderator">Sign in as Moderator</a></div>

authorization-roles/views/layout.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@{layout('')}
2+
3+
<!DOCTYPE html>
4+
<html>
5+
<head>
6+
<title>Authorization with roles</title>
7+
<meta charset="utf-8" />
8+
<meta http-equiv="X-UA-Compatible" content="IE=10" />
9+
<meta name="format-detection" content="telephone=no"/>
10+
<meta name="viewport" content="width=1024, user-scalable=yes" />
11+
<meta name="robots" content="all,follow" />
12+
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
13+
<style type="text/css">
14+
body { padding: 50px; margin: 0; font:normal 12px Arial; color: gray }
15+
</style>
16+
</head>
17+
<body>
18+
@{body}
19+
</body>
20+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>moderator.html: @{user.name}</h1>
2+
<div><a href="/logoff/">Sign out</a></div>

0 commit comments

Comments
 (0)
0