2014-03-14 21:36:45 +04:00
var sequence = require ( 'when/sequence' ) ,
_ = require ( 'lodash' ) ,
2014-07-10 14:00:51 +04:00
utils = require ( '../../utils' ) ,
2014-03-14 21:36:45 +04:00
Post = require ( '../../models/post' ) . Post ,
Tag = require ( '../../models/tag' ) . Tag ,
Role = require ( '../../models/role' ) . Role ,
Permission = require ( '../../models/permission' ) . Permission ,
2014-06-30 16:58:10 +04:00
Client = require ( '../../models/client' ) . Client ,
2014-03-14 21:36:45 +04:00
Permissions = require ( '../../models/permission' ) . Permissions ,
2014-07-10 14:00:51 +04:00
User = require ( '../../models/user' ) . User ,
2014-03-14 21:36:45 +04:00
populateFixtures ,
updateFixtures ;
2013-06-15 18:10:30 +04:00
2013-09-15 02:14:39 +04:00
var fixtures = {
2013-05-17 01:16:09 +04:00
posts : [
{
2013-07-12 02:10:19 +04:00
"title" : "Welcome to Ghost" ,
"slug" : "welcome-to-ghost" ,
2013-12-19 19:05:12 +04:00
"markdown" : "You're live! Nice. We've put together a little post to introduce you to the Ghost editor and get you started. You can manage your content by signing in to the admin area at `<your blog URL>/ghost/`. When you arrive, you can select this post from a list on the left and see a preview of it on the right. Click the little pencil icon at the top of the preview to edit this post and read the next section!\n\n## Getting Started\n\nGhost uses something called Markdown for writing. Essentially, it's a shorthand way to manage your post formatting as you write!\n\nWriting in Markdown is really easy. In the left hand panel of Ghost, you simply write as you normally would. Where appropriate, you can use *shortcuts* to **style** your content. For example, a list:\n\n* Item number one\n* Item number two\n * A nested item\n* A final item\n\nor with numbers!\n\n1. Remember to buy some milk\n2. Drink the milk\n3. Tweet that I remembered to buy the milk, and drank it\n\n### Links\n\nWant to link to a source? No problem. If you paste in url, like http://ghost.org - it'll automatically be linked up. But if you want to customise your anchor text, you can do that too! Here's a link to [the Ghost website](http://ghost.org). Neat.\n\n### What about Images?\n\nImages work too! Already know the URL of the image you want to include in your article? Simply paste it in like this to make it show up:\n\n![The Ghost Logo](https://ghost.org/images/ghost.png)\n\nNot sure which image you want to use yet? That's ok too. Leave yourself a descriptive placeholder and keep writing. Come back later and drag and drop the image in to upload:\n\n![A bowl of bananas]\n\n\n### Quoting\n\nSometimes a link isn't enough, you want to quote someone on what they've said. It was probably very wisdomous. Is wisdomous a word? Find out in a future release when we introduce spellcheck! For now - it's definitely a word.\n\n> Wisdomous - it's definitely a word.\n\n### Working with Code\n\nGot a streak of geek? We've got you covered there, too. You can write inline `<code>` blocks really easily with back ticks. Want to show off something more comprehensive? 4 spaces of indentation gets you there.\n\n .awesome-thing {\n display: block;\n width: 100%;\n }\n\n### Ready for a Break? \n\nThrow 3 or more dashes down on any new line and you've got yourself a fancy new divider. Aw yeah.\n\n---\n\n### Advanced Usage\n\nThere's one fantastic secret about Markdown. If you want, you can write plain old HTML and it'll still work! Very flexible.\n\n<input type=\"text\" placeholder=\"I'm an input field!\" />\n\nThat should be enough to get you started. Have fun - and let us know what you think :)" ,
2013-07-12 02:10:19 +04:00
"image" : null ,
2013-09-12 02:04:49 +04:00
"featured" : false ,
"page" : false ,
2013-06-07 00:36:17 +04:00
"status" : "published" ,
2013-09-12 02:04:49 +04:00
"language" : "en_US" ,
"meta_title" : null ,
2013-09-15 02:14:39 +04:00
"meta_description" : null
2013-05-17 01:16:09 +04:00
}
] ,
2013-09-19 08:59:33 +04:00
tags : [
{
"name" : "Getting Started" ,
"slug" : "getting-started" ,
"description" : null ,
"parent_id" : null ,
"meta_title" : null ,
"meta_description" : null
}
] ,
2013-06-05 07:47:11 +04:00
roles : [
{
2013-09-15 02:14:39 +04:00
"name" : "Administrator" ,
"description" : "Administrators"
2013-06-05 07:47:11 +04:00
} ,
{
2013-09-15 02:14:39 +04:00
"name" : "Editor" ,
"description" : "Editors"
2013-06-05 07:47:11 +04:00
} ,
{
2013-09-15 02:14:39 +04:00
"name" : "Author" ,
"description" : "Authors"
2013-06-05 07:47:11 +04:00
}
] ,
permissions : [
{
2013-09-15 02:14:39 +04:00
"name" : "Edit posts" ,
"action_type" : "edit" ,
"object_type" : "post"
2013-06-05 07:47:11 +04:00
} ,
{
2013-09-15 02:14:39 +04:00
"name" : "Remove posts" ,
"action_type" : "remove" ,
"object_type" : "post"
2013-06-05 07:47:11 +04:00
} ,
{
2013-09-15 02:14:39 +04:00
"name" : "Create posts" ,
"action_type" : "create" ,
"object_type" : "post"
2013-06-05 07:47:11 +04:00
}
2014-03-14 21:36:45 +04:00
] ,
permissions003 : [
{
2014-05-06 02:38:05 +04:00
"name" : "Generate post slug" ,
"action_type" : "generate" ,
"object_type" : "slug"
} ,
{
"name" : "Generate tag slug" ,
"action_type" : "generate" ,
"object_type" : "slug"
2014-03-14 21:36:45 +04:00
} ,
{
"name" : "Export database" ,
"action_type" : "exportContent" ,
"object_type" : "db"
} ,
{
"name" : "Import database" ,
"action_type" : "importContent" ,
"object_type" : "db"
} ,
{
"name" : "Delete all content" ,
"action_type" : "deleteAllContent" ,
"object_type" : "db"
} ,
{
"name" : "Browse users" ,
"action_type" : "browse" ,
"object_type" : "user"
} ,
{
"name" : "Read users" ,
"action_type" : "read" ,
"object_type" : "user"
} ,
{
"name" : "Edit users" ,
"action_type" : "edit" ,
"object_type" : "user"
} ,
{
"name" : "Add users" ,
"action_type" : "add" ,
"object_type" : "user"
} ,
2014-06-20 13:15:01 +04:00
{
"name" : "Remove users" ,
"action_type" : "remove" ,
"object_type" : "user"
} ,
2014-03-14 21:36:45 +04:00
{
"name" : "Browse settings" ,
"action_type" : "browse" ,
"object_type" : "setting"
} ,
{
"name" : "Read settings" ,
"action_type" : "read" ,
"object_type" : "setting"
} ,
{
"name" : "Edit settings" ,
"action_type" : "edit" ,
"object_type" : "setting"
2014-05-13 18:33:34 +04:00
} ,
{
"name" : "Browse themes" ,
"action_type" : "browse" ,
"object_type" : "theme"
} ,
{
"name" : "Edit themes" ,
"action_type" : "edit" ,
"object_type" : "theme"
2014-03-14 21:36:45 +04:00
}
2014-06-30 16:58:10 +04:00
] ,
client003 : [
{
"name" : "Ghost Admin" ,
"slug" : "ghost-admin" ,
"secret" : "not_available"
} ,
2014-07-10 14:00:51 +04:00
] ,
roles003 : [
{
"name" : "Owner" ,
"description" : "Owners"
}
] ,
users003 : [
{
"name" : "Owner" ,
"email" : "ghost@ghost.org" ,
"status" : "inactive" ,
"password" : utils . uid ( 50 ) ,
}
2013-05-17 01:16:09 +04:00
]
2013-09-02 05:49:08 +04:00
} ;
2013-09-15 02:14:39 +04:00
2014-03-14 21:36:45 +04:00
populateFixtures = function ( ) {
var ops = [ ] ,
relations = [ ] ;
_ . each ( fixtures . posts , function ( post ) {
2014-04-03 17:03:09 +04:00
ops . push ( function ( ) { return Post . add ( post , { user : 1 } ) ; } ) ;
2014-03-14 21:36:45 +04:00
} ) ;
_ . each ( fixtures . tags , function ( tag ) {
2014-04-03 17:03:09 +04:00
ops . push ( function ( ) { return Tag . add ( tag , { user : 1 } ) ; } ) ;
2014-03-14 21:36:45 +04:00
} ) ;
_ . each ( fixtures . roles , function ( role ) {
2014-04-03 17:03:09 +04:00
ops . push ( function ( ) { return Role . add ( role , { user : 1 } ) ; } ) ;
2014-03-14 21:36:45 +04:00
} ) ;
2014-07-10 14:00:51 +04:00
_ . each ( fixtures . roles003 , function ( role ) {
ops . push ( function ( ) { return Role . add ( role , { user : 1 } ) ; } ) ;
} ) ;
2014-03-14 21:36:45 +04:00
_ . each ( fixtures . permissions , function ( permission ) {
2014-04-03 17:03:09 +04:00
ops . push ( function ( ) { return Permission . add ( permission , { user : 1 } ) ; } ) ;
2014-03-14 21:36:45 +04:00
} ) ;
_ . each ( fixtures . permissions003 , function ( permission ) {
2014-04-03 17:03:09 +04:00
ops . push ( function ( ) { return Permission . add ( permission , { user : 1 } ) ; } ) ;
2014-03-14 21:36:45 +04:00
} ) ;
2014-06-30 16:58:10 +04:00
_ . each ( fixtures . client003 , function ( client ) {
ops . push ( function ( ) { return Client . add ( client , { user : 1 } ) ; } ) ;
} ) ;
2014-03-14 21:36:45 +04:00
// add the tag to the post
relations . push ( function ( ) {
Post . forge ( { id : 1 } ) . fetch ( { withRelated : [ 'tags' ] } ) . then ( function ( post ) {
post . tags ( ) . attach ( [ 1 ] ) ;
} ) ;
} ) ;
//grant permissions to roles
relations . push ( function ( ) {
// admins gets all permissions
Role . forge ( { name : 'Administrator' } ) . fetch ( { withRelated : [ 'permissions' ] } ) . then ( function ( role ) {
Permissions . forge ( ) . fetch ( ) . then ( function ( perms ) {
var admin _perm = _ . map ( perms . toJSON ( ) , function ( perm ) {
return perm . id ;
} ) ;
return role . permissions ( ) . attach ( _ . compact ( admin _perm ) ) ;
} ) ;
2013-09-15 02:14:39 +04:00
} ) ;
2013-09-19 08:59:33 +04:00
2014-03-14 21:36:45 +04:00
// editor gets access to posts, users and settings.browse, settings.read
Role . forge ( { name : 'Editor' } ) . fetch ( { withRelated : [ 'permissions' ] } ) . then ( function ( role ) {
Permissions . forge ( ) . fetch ( ) . then ( function ( perms ) {
var editor _perm = _ . map ( perms . toJSON ( ) , function ( perm ) {
2014-05-06 02:38:05 +04:00
if ( perm . object _type === 'post' || perm . object _type === 'user' || perm . object _type === 'slug' ) {
2014-03-14 21:36:45 +04:00
return perm . id ;
}
if ( perm . object _type === 'setting' &&
( perm . action _type === 'browse' || perm . action _type === 'read' ) ) {
return perm . id ;
}
return null ;
} ) ;
return role . permissions ( ) . attach ( _ . compact ( editor _perm ) ) ;
} ) ;
2013-09-19 08:59:33 +04:00
} ) ;
2014-05-06 02:38:05 +04:00
// author gets access to post.add, slug.generate, settings.browse, settings.read, users.browse and users.read
2014-03-14 21:36:45 +04:00
Role . forge ( { name : 'Author' } ) . fetch ( { withRelated : [ 'permissions' ] } ) . then ( function ( role ) {
Permissions . forge ( ) . fetch ( ) . then ( function ( perms ) {
var author _perm = _ . map ( perms . toJSON ( ) , function ( perm ) {
2014-05-06 02:38:05 +04:00
if ( perm . object _type === 'post' && perm . action _type === 'add' ) {
return perm . id ;
}
if ( perm . object _type === 'slug' && perm . action _type === 'generate' ) {
2014-03-14 21:36:45 +04:00
return perm . id ;
}
if ( perm . object _type === 'setting' &&
( perm . action _type === 'browse' || perm . action _type === 'read' ) ) {
return perm . id ;
}
if ( perm . object _type === 'user' &&
( perm . action _type === 'browse' || perm . action _type === 'read' ) ) {
return perm . id ;
}
return null ;
} ) ;
return role . permissions ( ) . attach ( _ . compact ( author _perm ) ) ;
} ) ;
2013-09-15 02:14:39 +04:00
} ) ;
2014-03-14 21:36:45 +04:00
} ) ;
return sequence ( ops ) . then ( function ( ) {
2014-07-10 14:00:51 +04:00
return sequence ( relations ) ;
} ) . then ( function ( ) {
return Role . findOne ( { name : 'Owner' } ) ;
} ) . then ( function ( ownerRole ) {
var user = fixtures . users003 [ 0 ] ;
user . role = ownerRole . id ;
return User . add ( fixtures . users003 [ 0 ] , { user : 1 } ) ;
2014-03-14 21:36:45 +04:00
} ) ;
} ;
updateFixtures = function ( ) {
var ops = [ ] ,
2014-07-10 14:00:51 +04:00
relations = [ ] ,
adminUser ;
2014-03-14 21:36:45 +04:00
_ . each ( fixtures . permissions003 , function ( permission ) {
2014-04-03 17:03:09 +04:00
ops . push ( function ( ) { return Permission . add ( permission , { user : 1 } ) ; } ) ;
2014-06-30 16:58:10 +04:00
} ) ;
_ . each ( fixtures . client003 , function ( client ) {
ops . push ( function ( ) { return Client . add ( client , { user : 1 } ) ; } ) ;
2014-03-14 21:36:45 +04:00
} ) ;
2014-07-10 14:00:51 +04:00
_ . each ( fixtures . roles003 , function ( role ) {
ops . push ( function ( ) { return Role . add ( role , { user : 1 } ) ; } ) ;
} ) ;
2014-03-14 21:36:45 +04:00
relations . push ( function ( ) {
// admin gets all new permissions
Role . forge ( { name : 'Administrator' } ) . fetch ( { withRelated : [ 'permissions' ] } ) . then ( function ( role ) {
Permissions . forge ( ) . fetch ( ) . then ( function ( perms ) {
var admin _perm = _ . map ( perms . toJSON ( ) , function ( perm ) {
var result = fixtures . permissions003 . filter ( function ( object ) {
return object . object _type === perm . object _type && object . action _type === perm . action _type ;
} ) ;
if ( ! _ . isEmpty ( result ) ) {
return perm . id ;
}
return null ;
} ) ;
return role . permissions ( ) . attach ( _ . compact ( admin _perm ) ) ;
} ) ;
2013-09-15 02:14:39 +04:00
} ) ;
2014-03-14 21:36:45 +04:00
// editor gets access to posts, users and settings.browse, settings.read
Role . forge ( { name : 'Editor' } ) . fetch ( { withRelated : [ 'permissions' ] } ) . then ( function ( role ) {
Permissions . forge ( ) . fetch ( ) . then ( function ( perms ) {
var editor _perm = _ . map ( perms . toJSON ( ) , function ( perm ) {
if ( perm . object _type === 'post' || perm . object _type === 'user' ) {
return perm . id ;
}
if ( perm . object _type === 'setting' &&
( perm . action _type === 'browse' || perm . action _type === 'read' ) ) {
return perm . id ;
}
return null ;
} ) ;
return role . permissions ( ) . attach ( _ . compact ( editor _perm ) ) ;
2013-09-19 08:59:33 +04:00
} ) ;
} ) ;
2014-03-14 21:36:45 +04:00
// author gets access to post.add, post.slug, settings.browse, settings.read, users.browse and users.read
Role . forge ( { name : 'Author' } ) . fetch ( { withRelated : [ 'permissions' ] } ) . then ( function ( role ) {
Permissions . forge ( ) . fetch ( ) . then ( function ( perms ) {
var author _perm = _ . map ( perms . toJSON ( ) , function ( perm ) {
2014-05-06 02:38:05 +04:00
if ( perm . object _type === 'post' && perm . action _type === 'add' ) {
return perm . id ;
}
if ( perm . object _type === 'slug' && perm . action _type === 'generate' ) {
2014-03-14 21:36:45 +04:00
return perm . id ;
}
if ( perm . object _type === 'setting' &&
( perm . action _type === 'browse' || perm . action _type === 'read' ) ) {
return perm . id ;
}
if ( perm . object _type === 'user' &&
( perm . action _type === 'browse' || perm . action _type === 'read' ) ) {
return perm . id ;
}
return null ;
} ) ;
return role . permissions ( ) . attach ( _ . compact ( author _perm ) ) ;
2013-09-15 02:14:39 +04:00
} ) ;
} ) ;
2014-03-14 21:36:45 +04:00
} ) ;
2013-09-15 02:14:39 +04:00
2014-03-14 21:36:45 +04:00
return sequence ( ops ) . then ( function ( ) {
2014-07-10 14:00:51 +04:00
return sequence ( relations ) ;
} ) . then ( function ( ) {
return User . forge ( { id : 1 } ) . fetch ( ) ;
} ) . then ( function ( user ) {
adminUser = user ;
return Role . findOne ( { name : 'Owner' } ) ;
} ) . then ( function ( ownerRole ) {
if ( adminUser ) {
return adminUser . roles ( ) . updatePivot ( { role _id : ownerRole . id } ) ;
}
2014-03-14 21:36:45 +04:00
} ) ;
2014-02-27 06:44:09 +04:00
} ;
2014-03-14 21:36:45 +04:00
module . exports = {
populateFixtures : populateFixtures ,
updateFixtures : updateFixtures
2014-04-03 17:03:09 +04:00
} ;