[guadec-web-regcfp/develop: 3/3] Papers permissions working
- From: Patrick Uiterwijk <puiterwijk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [guadec-web-regcfp/develop: 3/3] Papers permissions working
- Date: Mon, 18 May 2015 13:51:34 +0000 (UTC)
commit 7117301208cd22da62be1d64dc31deeb520e1606
Author: Patrick Uiterwijk <puiterwijk redhat com>
Date: Mon May 18 15:43:22 2015 +0200
Papers permissions working
Signed-off-by: Patrick Uiterwijk <puiterwijk redhat com>
app.js | 4 ++-
config/config.json.example | 19 +++++-----------
routes/papers.js | 13 +++++++----
utils.js | 45 ++++++++++++++++++++++++++++++++++------
views/index/index.hbs | 48 ++++++++++++++++++++++++++++++++++++++-----
views/papers/list.hbs | 2 +-
6 files changed, 98 insertions(+), 33 deletions(-)
---
diff --git a/app.js b/app.js
index df73c80..a928a71 100644
--- a/app.js
+++ b/app.js
@@ -18,6 +18,7 @@ var app = express();
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/config/config.json')[env];
+var utils = require('./utils')
// view engine setup
var hbs = handlebars.create({
@@ -29,7 +30,8 @@ var hbs = handlebars.create({
text = handlebars.handlebars.Utils.escapeExpression(text);
text = text.replace(/(\r\n|\n|\r)/gm, '<br>');
return new handlebars.handlebars.SafeString(text);
- }
+ },
+ has_permission: utils.has_permission
}
});
app.set('views', path.join(__dirname, 'views'));
diff --git a/config/config.json.example b/config/config.json.example
index 32a8e3e..0bda0c0 100644
--- a/config/config.json.example
+++ b/config/config.json.example
@@ -9,28 +9,21 @@
"persona_audience": "http://localhost:3000",
"permissions": {
+ "admin": ["puiterwijk fedoraproject org"],
"papers": {
- "submit": ["*"],
+ "submit": ["*authenticated*"],
"list": {
- "accepted": ["*"],
+ "accepted": ["*anonymous*"],
"own": ["*authenticated*"],
- "all": ["puiterwijk gnome org"]
+ "all": ["puiterwijk fedoraproject org"]
},
- "vote": ["puiterwijk gnome org"],
- "admin": ["puiterwijk gnome org"]
+ "vote": ["puiterwijk fedoraproject org"],
+ "admin": ["puiterwijk fedoraproject org"]
}
},
"papers": {
"enabled": true,
- "submission_enabled": true,
- "voting_enabled": false,
- "accepted_public": false,
- "required_permissions": {
- "submit": "authenticated",
- "vote": "paper_committee",
- "admin": "paper_admin"
- }
},
"registration": {
diff --git a/routes/papers.js b/routes/papers.js
index a85cb08..45fa28d 100644
--- a/routes/papers.js
+++ b/routes/papers.js
@@ -112,14 +112,15 @@ router.get('/list/own', function(req, res, next) {
res.render('papers/list', { papers: [] });
} else {
user.getPapers().complete(function(err, papers) {
- res.render('papers/list', { papers: papers });
+ res.render('papers/list', { description: 'Your',
+ papers: papers });
});
}
});
});
-router.all('/list/accepted', utils.require_permission('papers/list/accepted'));
-router.get('/list/accepted', function(req, res, next) {
+router.all('/list', utils.require_permission('papers/list/accepted'));
+router.get('/list', function(req, res, next) {
Paper
.find({
where: {
@@ -127,7 +128,8 @@ router.get('/list/accepted', function(req, res, next) {
}
})
.complete(function(err, papers) {
- res.render('papers/list', { papers: papers });
+ res.render('papers/list', { description: 'Accepted',
+ papers: papers });
});
});
@@ -135,7 +137,8 @@ router.all('/admin/list', utils.require_permission('papers/list/all'));
router.get('/admin/list', function(req, res, next) {
Paper.find()
.complete(function(err, papers) {
- res.render('papers/list', { papers: papers });
+ res.render('papers/list', { description: 'All',
+ papers: papers });
});
});
diff --git a/utils.js b/utils.js
index 13f4804..1121d87 100644
--- a/utils.js
+++ b/utils.js
@@ -3,7 +3,7 @@ var utils = {}
var env = process.env.NODE_ENV || "development";
var config = require(__dirname + '/config/config.json')[env];
-utils.require_permission = function(permission) {
+function get_permission_checker(permission) {
var required = permission.split('/');
var allowed = config.permissions;
for(var i = 0; i < required.length; i++)
@@ -11,16 +11,47 @@ utils.require_permission = function(permission) {
allowed = allowed[required[i]];
}
- return function(req, res, next) {
- if(allowed.indexOf('*') != -1)
+ // This function return is so we can make it optimized for require_permission
+ return function(username) {
+ if(allowed.indexOf('*anonymous*') != -1)
{
- next();
+ return true;
}
- else if((allowed.indexOf('*authenticated*') != -1) && (req.session.currentUser != null))
+ else if((allowed.indexOf('*authenticated*') != -1) && (username != null))
{
- next();
+ return true;
+ }
+ else if(allowed.indexOf(username) != -1)
+ {
+ return true;
}
- else if(allowed.indexOf(req.session.currentUser) != -1)
+ else
+ {
+ return false;
+ }
+ };
+};
+
+utils.has_permission = function(permission, options) {
+ console.log("Current:");
+ console.log(options);
+ console.log("Param:");
+ console.log(permission);
+ if(get_permission_checker(permission, options.data.root.session.currentUser))
+ {
+ return options.fn(this);
+ }
+ else
+ {
+ return options.inverse(this);
+ }
+};
+
+utils.require_permission = function(permission) {
+ var check_function = get_permission_checker(permission);
+
+ return function(req, res, next) {
+ if(check_function(req.session.currentUser))
{
next();
}
diff --git a/views/index/index.hbs b/views/index/index.hbs
index 840c496..ece8dc4 100644
--- a/views/index/index.hbs
+++ b/views/index/index.hbs
@@ -2,13 +2,49 @@
{{#if session.currentUser}}
Welcome, {{name}}<br />
-{{#if config.paper_submission_enabled}}
+{{else}}
+Hello, please login, or choose an option below.
+{{/if}}
+
+{{#if config.papers.enabled}}
+
+<h2>Papers management</h2>
<div class="buttons">
+{{#has_permission "papers/submit" }}
<div class="button"><a href="/papers/submit">Submit a talk</a></div>
-{{/if}}
-<div class="button"><a href="/papers/list">View your submitted talks</a></div>
+{{/has_permission}}
+{{#has_permission "papers/list/own" }}
+<div class="button"><a href="/papers/list/own">View your submitted talks</a></div>
+{{/has_permission}}
+{{#has_permission "papers/list/accepted" }}
+<div class="button"><a href="/papers/list">View accepted talks</a></div>
+{{/has_permission}}
</div>
-Registering is not yet functional
-{{else}}
-Hello, please login, as the anonymous parts are not yet operational
+
+{{/if}}
+
+
+
+{{#if config.registration.enabled}}
+
+<h2>Registration</h2>
+.....
+
{{/if}}
+
+
+{{#has_permission "admin" }}
+<h2>Admin</h2>
+
+<h3>Papers management</h3>
+<div class="buttons">
+{{#has_permission "papers/list/all"}}
+<div class="button"><a href="/papers/admin/list">View all talks</a></div>
+{{/has_permission}}
+</div>
+
+<h3>Registration</h3>
+<div class="buttons">
+</div>
+
+{{/has_permission}}
diff --git a/views/papers/list.hbs b/views/papers/list.hbs
index 53b99c5..e92c7c4 100644
--- a/views/papers/list.hbs
+++ b/views/papers/list.hbs
@@ -1,4 +1,4 @@
-Your submitted papers:<br /><br />
+{{description}} submitted papers:<br /><br />
{{#each papers}}
<b>{{ this.title }}</b>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]