Skip to content

Commit

Permalink
Merge pull request #682 from Automattic/fix/improved-file-mod-permiss…
Browse files Browse the repository at this point in the history
…ions

Site Utils: Added canUpdateFiles method to site utils.
  • Loading branch information
enejb committed Nov 26, 2015
2 parents 5dd7d73 + 922660f commit 758c146
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
3 changes: 2 additions & 1 deletion client/lib/site/jetpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var wpcom = require( 'lib/wp' ),
notices = require( 'notices' ),
i18n = require( 'lib/mixins/i18n' ),
versionCompare = require( 'lib/version-compare' ),
SiteUtils = require( 'lib/site/utils' ),
config = require( 'config' );

inherits( JetpackSite, Site );
Expand All @@ -31,7 +32,7 @@ JetpackSite.prototype.updateComputedAttributes = function() {
// unmapped_url is more likely to equal main_network_site because they should both be set to siteurl option
// is_multi_network checks to see that a site is not part of a multi network
// Since there is no primary network we disable updates for that case
this.canUpdateFiles = this.hasMinimumJetpackVersion && this.options.unmapped_url === this.options.main_network_site && ! this.options.is_multi_network && ! this.options.file_mod_disabled;
this.canUpdateFiles = SiteUtils.canUpdateFiles( this );
this.hasJetpackMenus = versionCompare( this.options.jetpack_version, '3.5-alpha' ) >= 0;
this.hasJetpackThemes = versionCompare( this.options.jetpack_version, '3.7-beta' ) >= 0;
};
Expand Down
57 changes: 57 additions & 0 deletions client/lib/site/test/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* External dependencies
*/
import chai from 'chai';

/**
* Internal dependencies
*/
import SiteUtils from 'lib/site/utils';

const assert = chai.assert;

describe( 'Site Utils', function() {
describe( 'canUpdateFiles', function() {
it( 'Should have a method canUpdateFiles.', function() {
assert.isFunction( SiteUtils.canUpdateFiles );
} );

it( 'CanUpdateFiles should return false when no site object is passed in.', function() {
assert.isFalse( SiteUtils.canUpdateFiles() );
} );

it( 'CanUpdateFiles should return false when passed an empty object.', function() {
assert.isFalse( SiteUtils.canUpdateFiles( {} ) );
} );

it( 'CanUpdateFiles should return false when passed an object without options.', function() {
assert.isFalse( SiteUtils.canUpdateFiles( { hello: 'not important' } ) );
} );

it( 'CanUpdateFiles should return false when passed an site object that has something value in the file_mod_option.', function() {
const site = {
hasMinimumJetpackVersion: true,
options: {
unmapped_url: 'someurl',
main_network_site: 'someurl',
is_multi_network: false,
file_mod_disabled: [ 'something else' ]
}
}
assert.isFalse( SiteUtils.canUpdateFiles( site ) );
} );

it( 'CanUpdateFiles should return true when passed a site data has all the right settings permissions to be able to update files.', function() {
const site = {
hasMinimumJetpackVersion: true,
options: {
unmapped_url: 'someurl',
main_network_site: 'someurl',
is_multi_network: false,
file_mod_disabled: false
}
}
assert.isTrue( SiteUtils.canUpdateFiles( site ) );
} );
} );
} );
28 changes: 23 additions & 5 deletions client/lib/site/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ export default {
i18n.translate( 'Core autoupdates are explicitly disabled by a site administrator.' )
);
break
case 'disallow_file_edit':
reasons.push(
i18n.translate( 'File edits are explicitly disabled by a site administrator.' )
);
break;
case 'disallow_file_mods':
reasons.push(
i18n.translate( 'File modifications are explicitly disabled by a site administrator.' )
Expand All @@ -104,5 +99,28 @@ export default {
}
}
return reasons;
},

canUpdateFiles( site ) {
if ( ! site ) {
return false;
}
if ( ! site.hasMinimumJetpackVersion ) {
return false;
}

if ( site.options && site.options.unmapped_url !== site.options.main_network_site ) {
return false;
}

if ( site.options.is_multi_network ) {
return false;
}

if ( site.options.file_mod_disabled ) {
return false;
}

return true;
}
};

0 comments on commit 758c146

Please sign in to comment.