Skip to content

Commit

Permalink
MM-12346: Checks permissions to edit post regardless of license. (mat…
Browse files Browse the repository at this point in the history
…termost#673)

* MM-12346: Checks permissions to edit post regardless of license.

* MM-12346: Check license for timed editing.
  • Loading branch information
Martin Kraft authored Oct 11, 2018
1 parent 00a9cf1 commit 4b1fadb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
37 changes: 17 additions & 20 deletions src/utils/post_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,27 @@ export function canEditPost(state, config, license, teamId, channelId, userId, p

let canEdit = true;

if (canEdit && license.IsLicensed === 'true') {
if (hasNewPermissions(state)) {
canEdit = canEdit && haveIChannelPermission(state, {team: teamId, channel: channelId, permission: Permissions.EDIT_POST});
if (!isOwner) {
canEdit = canEdit && haveIChannelPermission(state, {team: teamId, channel: channelId, permission: Permissions.EDIT_OTHERS_POSTS});
}
if (config.PostEditTimeLimit !== '-1' && config.PostEditTimeLimit !== -1) {
const timeLeft = (post.create_at + (config.PostEditTimeLimit * 1000)) - Date.now();
if (timeLeft <= 0) {
canEdit = false;
}
}
} else {
canEdit = isOwner && config.AllowEditPost !== 'never';
if (config.AllowEditPost === General.ALLOW_EDIT_POST_TIME_LIMIT) {
const timeLeft = (post.create_at + (config.PostEditTimeLimit * 1000)) - Date.now();
if (timeLeft <= 0) {
canEdit = false;
}
if (hasNewPermissions(state)) {
canEdit = canEdit && haveIChannelPermission(state, {team: teamId, channel: channelId, permission: Permissions.EDIT_POST});
if (!isOwner) {
canEdit = canEdit && haveIChannelPermission(state, {team: teamId, channel: channelId, permission: Permissions.EDIT_OTHERS_POSTS});
}
if (license.IsLicensed === 'true' && config.PostEditTimeLimit !== '-1' && config.PostEditTimeLimit !== -1) {
const timeLeft = (post.create_at + (config.PostEditTimeLimit * 1000)) - Date.now();
if (timeLeft <= 0) {
canEdit = false;
}
}
} else {
canEdit = canEdit && isOwner;
canEdit = isOwner && config.AllowEditPost !== 'never';
if (config.AllowEditPost === General.ALLOW_EDIT_POST_TIME_LIMIT) {
const timeLeft = (post.create_at + (config.PostEditTimeLimit * 1000)) - Date.now();
if (timeLeft <= 0) {
canEdit = false;
}
}
}

return canEdit;
}

Expand Down
12 changes: 7 additions & 5 deletions test/utils/post_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,15 @@ describe('PostUtils', () => {
const channelId = 'channel-id';
const userId = 'user-id';

const state = {entities: {general: {serverVersion: ''}}};

it('should allow to edit my post without license', () => {
// Hasn't license
assert.ok(canEditPost({}, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: userId, type: 'normal'}));
assert.ok(!canEditPost({}, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: userId, type: 'system_test'}));
assert.ok(!canEditPost({}, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: 'other', type: 'normal'}));
assert.ok(!canEditPost({}, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: 'other', type: 'system_test'}));
assert.ok(!canEditPost({}, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, null));
assert.ok(canEditPost(state, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: userId, type: 'normal'}));
assert.ok(!canEditPost(state, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: userId, type: 'system_test'}));
assert.ok(!canEditPost(state, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: 'other', type: 'normal'}));
assert.ok(!canEditPost(state, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, {user_id: 'other', type: 'system_test'}));
assert.ok(!canEditPost(state, {PostEditTimeLimit: -1}, notLicensed, teamId, channelId, userId, null));
});

it('should work with old permissions version', () => {
Expand Down

0 comments on commit 4b1fadb

Please sign in to comment.