Skip to content

Commit

Permalink
Adds admin backend for #53
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoltan Olah committed Oct 27, 2014
1 parent 877b94c commit b0b4ef6
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 4 deletions.
15 changes: 15 additions & 0 deletions client/templates/admin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template name="admin">
{{#if isAdmin}}
<h1>Admin</h1>

<a href="/">Home</a>

<form>
<label>Latest News</label>
<input name="text" type="text" value="{{latestNews.text}}" />
<button type="submit">Save</button>
</form>
{{else}}
<h1>Forbidden</h1>
{{/if}}
</template>
20 changes: 20 additions & 0 deletions client/templates/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Template.admin.helpers({
isAdmin: function() {
return Meteor.user() && Meteor.user().admin;
},

latestNews: function() {
return News.latest();
}
});

Template.admin.events({
'submit form': function(event) {
event.preventDefault();

var text = $(event.target).find('[name=text]').val();
News.insert({ text: text, date: new Date });

alert('Saved latest news');
}
})
3 changes: 3 additions & 0 deletions client/templates/app-body.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<div class="description-welcome">
When running this example on a physical phone you won't see the device chrome we've included here.
</div>
<div class="description-welcome">
The first authenticated user is allowed access to the <a href="{{pathFor 'admin'}}">/admin</a> backend.
</div>
</div>

<div id="container" class="{{menuOpen}} {{overlayOpen}}">
Expand Down
2 changes: 1 addition & 1 deletion client/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1 class="title-home">

<div class="callout-news">
<span class="title-callout">News</span>
{{latestNews}}
{{latestNews.text}}
</div>

<h2 class="list-title">Our Favorite Recipes</h2>
Expand Down
3 changes: 1 addition & 2 deletions client/templates/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ Template.home.helpers({
},

latestNews: function() {
var news = News.findOne({}, {sort: {date: -1}, limit: 1});
return news && news.text;
return News.latest();
}
});
11 changes: 11 additions & 0 deletions lib/news.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
News = new Mongo.Collection('news');

News.allow({
insert: function(userId) {
var user = Meteor.users.findOne(userId);
return user && user.admin;
}
});

News.latest = function() {
return News.findOne({}, {sort: {date: -1}, limit: 1});
}

if (Meteor.isServer && News.find().count() === 0) {
Meteor.startup(function() {
News.insert({
Expand Down
7 changes: 7 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@ RecipeController = RouteController.extend({
}
});

AdminController = RouteController.extend({
onBeforeAction: function() {
Meteor.subscribe('news');
}
});

Router.map(function() {
this.route('home', {path: '/'});
this.route('feed');
this.route('recipes');
this.route('bookmarks');
this.route('about');
this.route('recipe', {path: '/recipes/:name'});
this.route('admin', { layoutTemplate: null });
});

Router.onBeforeAction('dataNotFound', {only: 'recipe'});
3 changes: 2 additions & 1 deletion server/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ Meteor.publish('recipe', function(name) {
];
});

// autopublish the user's bookmarks
// autopublish the user's bookmarks and admin status
Meteor.publish(null, function() {
return Meteor.users.find(this.userId, {
fields: {
admin: 1,
bookmarkedRecipeNames: 1,
'services.twitter.profile_image_url_https': 1
}
Expand Down
10 changes: 10 additions & 0 deletions server/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Accounts.onCreateUser(function(options, user) {
if (options.profile)
user.profile = options.profile;

// If this is the first user going into the database, make them an admin
if (Meteor.users.find().count() === 0)
user.admin = true;

return user;
});

0 comments on commit b0b4ef6

Please sign in to comment.