Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 38fd0b6f6ecc796e7a03a6a0ff90426b3e6a860e 0 parents
@beausmith beausmith authored
77 LICENSE.md
@@ -0,0 +1,77 @@
+## Preamble
+
+This license establishes the terms under which a given free software Package may be copied, modified, distributed, and/or redistributed. The intent is that the Copyright Holder maintains some artistic control over the development of that Package while still keeping the Package available as open source and free software.
+
+You are always permitted to make arrangements wholly outside of this license directly with the Copyright Holder of a given Package. If the terms of this license do not permit the full use that you propose to make of the Package, you should contact the Copyright Holder and seek a different licensing arrangement.
+
+## Definitions
+
+"Copyright Holder" means the individual(s) or organization(s) named in the copyright notice for the entire Package.
+
+"Contributor" means any party that has contributed code or other material to the Package, in accordance with the Copyright Holder's procedures.
+
+"You" and "your" means any person who would like to copy, distribute, or modify the Package.
+
+"Package" means the collection of files distributed by the Copyright Holder, and derivatives of that collection and/or of those files. A given Package may consist of either the Standard Version, or a Modified Version.
+
+"Distribute" means providing a copy of the Package or making it accessible to anyone else, or in the case of a company or organization, to others outside of your company or organization.
+
+"Distributor Fee" means any fee that you charge for Distributing this Package or providing support for this Package to another party. It does not mean licensing fees.
+
+"Standard Version" refers to the Package if it has not been modified, or has been modified only in ways explicitly requested by the Copyright Holder.
+
+"Modified Version" means the Package, if it has been changed, and such changes were not explicitly requested by the Copyright Holder.
+
+"Original License" means this Artistic License as Distributed with the Standard Version of the Package, in its current version or as it may be modified by The Perl Foundation in the future.
+
+"Source" form means the source code, documentation source, and configuration files for the Package.
+
+"Compiled" form means the compiled bytecode, object code, binary, or any other form resulting from mechanical transformation or translation of the Source form.
+
+## Permission for Use and Modification Without Distribution
+
+(1) You are permitted to use the Standard Version and create and use Modified Versions for any purpose without restriction, provided that you do not Distribute the Modified Version.
+
+## Permissions for Redistribution of the Standard Version
+
+(2) You may Distribute verbatim copies of the Source form of the Standard Version of this Package in any medium without restriction, either gratis or for a Distributor Fee, provided that you duplicate all of the original copyright notices and associated disclaimers. At your discretion, such verbatim copies may or may not include a Compiled form of the Package.
+
+(3) You may apply any bug fixes, portability changes, and other modifications made available from the Copyright Holder. The resulting Package will still be considered the Standard Version, and as such will be subject to the Original License.
+
+## Distribution of Modified Versions of the Package as Source
+
+(4) You may Distribute your Modified Version as Source (either gratis or for a Distributor Fee, and with or without a Compiled form of the Modified Version) provided that you clearly document how it differs from the Standard Version, including, but not limited to, documenting any non-standard features, executables, or modules, and provided that you do at least ONE of the following:
+
+(a) make the Modified Version available to the Copyright Holder of the Standard Version, under the Original License, so that the Copyright Holder may include your modifications in the Standard Version.
+(b) ensure that installation of your Modified Version does not prevent the user installing or running the Standard Version. In addition, the Modified Version must bear a name that is different from the name of the Standard Version.
+(c) allow anyone who receives a copy of the Modified Version to make the Source form of the Modified Version available to others under
+(i) the Original License or
+(ii) a license that permits the licensee to freely copy, modify and redistribute the Modified Version using the same licensing terms that apply to the copy that the licensee received, and requires that the Source form of the Modified Version, and of any works derived from it, be made freely available in that license fees are prohibited but Distributor Fees are allowed.
+Distribution of Compiled Forms of the Standard Version or Modified Versions without the Source
+
+(5) You may Distribute Compiled forms of the Standard Version without the Source, provided that you include complete instructions on how to get the Source of the Standard Version. Such instructions must be valid at the time of your distribution. If these instructions, at any time while you are carrying out such distribution, become invalid, you must provide new instructions on demand or cease further distribution. If you provide valid instructions or cease distribution within thirty days after you become aware that the instructions are invalid, then you do not forfeit any of your rights under this license.
+
+(6) You may Distribute a Modified Version in Compiled form without the Source, provided that you comply with Section 4 with respect to the Source of the Modified Version.
+
+## Aggregating or Linking the Package
+
+(7) You may aggregate the Package (either the Standard Version or Modified Version) with other packages and Distribute the resulting aggregation provided that you do not charge a licensing fee for the Package. Distributor Fees are permitted, and licensing fees for other components in the aggregation are permitted. The terms of this license apply to the use and Distribution of the Standard or Modified Versions as included in the aggregation.
+
+(8) You are permitted to link Modified and Standard Versions with other works, to embed the Package in a larger work of your own, or to build stand-alone binary or bytecode versions of applications that include the Package, and Distribute the result without restriction, provided the result does not expose a direct interface to the Package.
+
+## Items That are Not Considered Part of a Modified Version
+
+(9) Works (including, but not limited to, modules and scripts) that merely extend or make use of the Package, do not, by themselves, cause the Package to be a Modified Version. In addition, such works are not considered parts of the Package itself, and are not subject to the terms of this license.
+
+## General Provisions
+
+(10) Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
+
+(11) If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
+
+(12) This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
+
+(13) This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
+
+(14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
35 README.md
@@ -0,0 +1,35 @@
+# Locksmith Plugin for Movable Type
+
+Authors: Six Apart, Kevin Shay
+Copyright 2009 Six Apart, Ltd.
+License: Artistic, licensed under the same terms as Perl itself
+
+## Overview
+
+A plugin for Movable Type which prevents entries and templates from being edited by more than one user at the same time.
+
+## Features
+
+* Entry/page locking to a single editor, entry's author, or disabled.
+* Template locking to a single editor or disabled.
+* Configurable times for:
+ * Lock duration when editing an object
+ * Lock renewal when editing an object
+ * Retry interval when viewing a locked entry
+* Lock override for System Administrator and optional second role.
+* All user messaging strings are configurable.
+
+## Installation
+
+1. Move the Locksmith plugin directory to the MT `plugins` directory.
+2. Move the Locksmith mt-static directory to the `mt-static/plugins` directory.
+
+Should look like this when installed:
+
+ $MT_HOME/
+ plugins/
+ Locksmith/
+ mt-static/
+ plugins/
+ Locksmith/
+
1  SUPPORT.md
@@ -0,0 +1 @@
+This plugin is not an official Six Apart release, and as such support from Six Apart for this plugin is not available.
220 mt-static/plugins/Locksmith/Locksmith.js
@@ -0,0 +1,220 @@
+var locksmith_disabled_fields;
+var locksmith_renewer;
+var locksmith_retryer;
+var locksmith_uri;
+var locksmith_object_type;
+var locksmith_object_id;
+
+document.getElementsByClassName = function(class_name) {
+ var docList = this.all || this.getElementsByTagName('*');
+ var matchArray = new Array();
+
+ /*Create a regular expression object for class*/
+ var re = new RegExp("(?:^|\\s)"+class_name+"(?:\\s|$)");
+ for (var i = 0; i < docList.length; i++) {
+ if (re.test(docList[i].className) ) {
+ matchArray[matchArray.length] = docList[i];
+ }
+ }
+ return matchArray;
+}
+
+function lockEntry() {
+ var form_elems = getByID('entry_form').elements;
+ // need to populate this first so we can unlock properly even if we don't lock
+ locksmith_disabled_fields = new Array();
+ for (var i = 0; i < form_elems.length; i++) {
+ if (form_elems[i].getAttribute('disabled')) {
+ locksmith_disabled_fields[form_elems[i].name] = true;
+ }
+ }
+ if (locksmith_override) {
+ var conf_text = locksmith_author_only ? locksmith_override_author_only_text : locksmith_override_text;
+ if (confirm(conf_text)) {
+ unlockEntry();
+ return;
+ }
+ }
+ for (var i = 0; i < form_elems.length; i++) {
+ disableField(form_elems[i]);
+ }
+ var ects = document.getElementsByClassName('field-buttons-formatting');
+ for (var i = 0; i < ects.length; i++) {
+ TC.addClassName(ects[i], 'hidden');
+ }
+ var pickers = document.getElementsByClassName('date-picker');
+ for (var i = 0; i < pickers.length; i++) {
+ TC.addClassName(pickers[i], 'hidden');
+ }
+ var buttons = document.getElementsByTagName('button');
+ for (var i = 0; i < buttons.length; i++) {
+ disableField(buttons[i]);
+ setOpacity(buttons[i], '.5');
+ }
+ var fd_button_spans = document.getElementsByClassName('fd-group-button');
+ for (var i = 0; i < fd_button_spans.length; i++) {
+ TC.addClassName(fd_button_spans[i], 'hidden');
+ }
+ App.bootstrap();
+ app.categorySelector.close(getByID('close-category-selector1'));
+ TC.addClassName(getByID('open-category-selector1'), 'hidden');
+ TC.removeClassName(getByID('locksmith-msg'), 'hidden');
+ clearInterval(locksmith_retryer);
+ clearInterval(locksmith_renewer);
+ if (!locksmith_read_only) {
+ locksmith_retryer = setInterval('retryLock();', locksmith_retry_every + 1000 * 60);
+ }
+}
+
+function unlockEntry() {
+// this is necessary on override because the browser may have the disabled state of fields
+// cached on a reload; we don't need to unhide other elements hidden by lockEntry()
+ getByID('editor-content-toolbar').childNodes[1].style.display = 'block';
+ var form_elems = getByID('entry_form').elements;
+ for (var i = 0; i < form_elems.length; i++) {
+ if (!locksmith_disabled_fields[form_elems[i].name]) {
+ enableField(form_elems[i]);
+ }
+ }
+ var buttons = document.getElementsByTagName('button');
+ for (var i = 0; i < buttons.length; i++) {
+ enableField(buttons[i]);
+ setOpacity(buttons[i], 1);
+ }
+ TC.addClassName(getByID('locksmith-msg'), 'hidden');
+ startRenewLock();
+}
+
+function lockTemplate() {
+ if (locksmith_override) {
+ if (confirm(locksmith_override_text)) {
+ // this is necessary because the browser may have the disabled state
+ // cached on a reload
+ unlockTemplate();
+ return;
+ }
+ }
+ if (window.app.cpeList) {
+ window.app.cpeList[0].toggleOff(false); // don't set a cookie
+ }
+ TC.addClassName(getByID('template-body-actions'), 'hidden');
+ var form_elems = getByID('template-listing-form').elements;
+ for (var i = 0; i < form_elems.length; i++) {
+ disableField(form_elems[i]);
+ }
+ var buttons = document.getElementsByTagName('button');
+ for (var i = 0; i < buttons.length; i++) {
+ disableField(buttons[i]);
+ setOpacity(buttons[i], '.5');
+ }
+ var create_link = document.getElementsByClassName('icon-create')[0];
+ if (create_link) {
+ TC.addClassName(create_link, 'hidden');
+ }
+ var delete_links = document.getElementsByClassName('delete-archive-link');
+ for (var i = 0; i < delete_links.length; i++) {
+ TC.addClassName(delete_links[i], 'hidden');
+ }
+ TC.removeClassName(getByID('locksmith-msg'), 'hidden');
+ locksmith_retryer = setInterval('retryLock();', locksmith_retry_every + 1000 * 60);
+}
+
+function unlockTemplate() {
+ var form_elems = getByID('template-listing-form').elements;
+ for (var i = 0; i < form_elems.length; i++) {
+ enableField(form_elems[i]);
+ }
+ var buttons = document.getElementsByTagName('button');
+ for (var i = 0; i < buttons.length; i++) {
+ enableField(buttons[i]);
+ setOpacity(buttons[i], 1);
+ }
+ TC.addClassName(getByID('locksmith-msg'), 'hidden');
+ startRenewLock();
+}
+
+function disableField(fld) {
+ fld.setAttribute('disabled', 'disabled');
+}
+
+function enableField(fld) {
+ fld.removeAttribute('disabled');
+}
+
+function setOpacity(elem, opacity) {
+ elem.style.opacity = opacity;
+ var op_n = parseFloat(opacity) * 100;
+ elem.filter = 'alpha(opacity=' + op_n + ')';
+}
+
+function startRenewLock(uri, object_type, object_id) {
+ clearInterval(locksmith_retryer);
+ clearInterval(locksmith_renewer);
+ locksmith_renewer = setInterval('renewLock();', locksmith_renew_every * 1000 * 60);
+ TC.attachWindowEvent('unload', releaseLock);
+}
+
+function renewLock() {
+ var param = '__mode=locksmith_renew_lock&object_type=' + locksmith_object_type + '&id=' + locksmith_object_id;
+ var params = {
+ uri: locksmith_uri,
+ method: 'POST',
+ arguments: param,
+ load: function(c) {
+ if (!c.responseText) return;
+ var resp;
+ try {
+ resp = eval('(' + c.responseText + ')');
+ } catch(e) {
+ alert("Error: invalid response");
+ return;
+ }
+ if (resp.error) {
+ alert(resp.error);
+ return;
+ }
+ locksmith_locked_until = resp.result.result;
+ }
+ };
+ TC.Client.call(params);
+}
+
+function retryLock() {
+ var param = '__mode=locksmith_retry_lock&object_type=' + locksmith_object_type + '&id=' + locksmith_object_id;
+ var params = {
+ uri: locksmith_uri,
+ method: 'POST',
+ arguments: param,
+ load: function(c) {
+ if (!c.responseText) return;
+ var resp;
+ try {
+ resp = eval('(' + c.responseText + ')');
+ } catch(e) {
+ alert("Error: invalid response");
+ return;
+ }
+ if (resp.error) {
+ alert(resp.error);
+ return;
+ }
+ if (resp.result.got_lock) {
+ if (confirm(locksmith_now_available_text)) {
+ window.location = window.location + '&locksmith_enable=1';
+ }
+ }
+ }
+ };
+ TC.Client.call(params);
+}
+
+function releaseLock() {
+ var param = '__mode=locksmith_release_lock&object_type=' + locksmith_object_type + '&id=' + locksmith_object_id + '&locked_until=' + locksmith_locked_until;
+ var params = {
+ uri: locksmith_uri,
+ method: 'POST',
+ arguments: param,
+ load: function(c) { }
+ };
+ TC.Client.call(params);
+}
69 plugins/Locksmith/config.yaml
@@ -0,0 +1,69 @@
+id: Locksmith
+name: Locksmith
+description: Access control
+version: 0.22
+schema_version: 0.1
+
+object_types:
+ objectlock: ObjectLock
+
+config_template: config.tmpl
+
+init_app: $Locksmith::Locksmith::Util::init_app
+
+settings:
+ entry_locking:
+ default: 1
+ template_locking:
+ default: 1
+ hold_for:
+ default: 5
+ renew_every:
+ default: 1
+ retry_every:
+ default: 5
+ override_role:
+ locked_text:
+ default: This <mt:var name="object_type"> is being edited by <$mt:AuthorDisplayName$>.
+ author_only_text:
+ default: This <mt:var name="object_type"> can only be edited by its author, <$mt:var name="entry_author_display_name"$>.
+ override_text:
+ default: Someone else is editing this <mt:var name="object_type">. Override lock?
+ override_author_only_text:
+ default: This <mt:var name="object_type"> can only be edited by its author. Override lock?
+ now_available_text:
+ default: This <mt:var name="object_type"> is now available for editing. Reload?
+ read_only_text:
+ default: Read-only
+
+permissions:
+ blog.edit_all_posts_read_only:
+ label: Read-Only
+ group: blog_admin
+ order: 1000
+
+applications:
+ cms:
+ methods:
+ locksmith_renew_lock:
+ handler: $Locksmith::Locksmith::CMS::renew_lock
+ requires_login: 0
+ locksmith_retry_lock:
+ handler: $Locksmith::Locksmith::CMS::retry_lock
+ requires_login: 0
+ locksmith_release_lock:
+ handler: $Locksmith::Locksmith::CMS::release_lock
+
+callbacks:
+ MT::App::CMS::template_source.edit_entry: $Locksmith::Locksmith::CMS::source_edit_entry
+ MT::App::CMS::template_param.edit_entry: $Locksmith::Locksmith::CMS::param_edit_entry
+ MT::App::CMS::template_source.edit_template: $Locksmith::Locksmith::CMS::source_edit_template
+ MT::App::CMS::template_param.edit_template: $Locksmith::Locksmith::CMS::param_edit_template
+ MT::App::CMS::template_source.edit_role: $Locksmith::Locksmith::CMS::source_edit_role
+
+tags:
+ block:
+ EntryLockingAuthor: $Locksmith::Locksmith::Tags::hdlr_entry_locking_author
+ TemplateLockingAuthor: $Locksmith::Locksmith::Tags::hdlr_template_locking_author
+ EntryIfAuthorOnly?: $Locksmith::Locksmith::Tags::hdlr_entry_if_author_only
+ EntryIfReadOnly?: $Locksmith::Locksmith::Tags::hdlr_entry_if_read_only
248 plugins/Locksmith/lib/Locksmith/CMS.pm
@@ -0,0 +1,248 @@
+
+package Locksmith::CMS;
+use strict;
+use Data::Dumper;
+use MT::Util qw( encode_js );
+
+sub post_save_entry {
+ my ($cb, $app, $entry) = @_;
+}
+
+sub source_edit_entry {
+ my ($cb, $app, $template) = @_;
+ my $config = MT->component('locksmith')->get_config_hash('blog:' . $app->param('blog_id'));
+ return unless ($config->{entry_locking});
+ my $old = q{<mt:unless name="new_object">};
+ my $new = _head_mtml('entry', $config);
+ $$template =~ s/$old/$old$new/;
+ $old = q{<div id="msg-block">};
+ $new = qq{
+ <mtapp:statusmsg
+ id="locksmith-msg"
+ class="info hidden">
+ <mt:EntryIfAuthorOnly>
+ $config->{author_only_text}
+ <mt:Else>
+ <mt:EntryIfReadOnly>
+ $config->{read_only_text}
+ <mt:Else>
+ <mt:EntryLockingAuthor>
+ $config->{locked_text}
+ </mt:EntryLockingAuthor>
+ </mt:EntryIfReadOnly>
+ </mt:EntryIfAuthorOnly>
+ </mtapp:statusmsg>
+ };
+ $$template =~ s/$old/$old$new/;
+}
+
+sub source_edit_template {
+ my ($cb, $app, $template) = @_;
+ my $config = MT->component('locksmith')->get_config_hash('blog:' . $app->param('blog_id'));
+ return unless ($config->{template_locking});
+ my $old = q{<mt:setvarblock name="html_body" append="1">};
+ my $new = _head_mtml('template', $config);
+ $$template =~ s/$old/$old$new/;
+ $old = q{<mt:setvarblock name="system_msg">};
+ $new = qq{
+ <mtapp:statusmsg
+ id="locksmith-msg"
+ class="info hidden">
+ <mt:TemplateLockingAuthor>
+ $config->{locked_text}
+ </mt:TemplateLockingAuthor>
+ </mtapp:statusmsg>
+ };
+ $$template =~ s/$old/$old$new/;
+}
+
+sub _head_mtml {
+ my ($object_type, $config) = @_;
+ my $func = 'lock' . ucfirst($object_type);
+ return qq{
+<mt:setvarblock name="html_head" append="1">
+<script type="text/javascript" src="<mt:var name="static_uri">plugins/Locksmith/Locksmith.js"></script>
+</mt:setvarblock>
+<script type="text/javascript">
+var locksmith_locked = <mt:if name="locksmith_locked">true<mt:else>false</mt:if>;
+var locksmith_locked_until = <mt:var name="locksmith_locked_until">;
+var locksmith_read_only = <mt:if name="locksmith_read_only">true<mt:else>false</mt:if>;
+var locksmith_extended = <mt:if name="locksmith_extended">true<mt:else>false</mt:if>;
+var locksmith_author_only = <mt:if name="locksmith_author_only">true<mt:else>false</mt:if>;
+var locksmith_override = <mt:if name="locksmith_override">true<mt:else>false</mt:if>;
+var locksmith_hold_for = <mt:var name="locksmith_hold_for">;
+var locksmith_renew_every = <mt:var name="locksmith_renew_every">;
+var locksmith_retry_every = <mt:var name="locksmith_retry_every">;
+var locksmith_object_type = '<mt:var name="object_type">';
+var locksmith_object_id = '<mt:var name="id">';
+var locksmith_uri = '<mt:var name="script_url">';
+var locksmith_override_text = '<mt:Section encode_js="1">$config->{override_text}</mt:Section>';
+var locksmith_override_author_only_text = '<mt:Section encode_js="1">$config->{override_author_only_text}</mt:Section>';
+var locksmith_read_only_text = '<mt:Section encode_js="1">$config->{read_only_text}</mt:Section>';
+var locksmith_now_available_text = '<mt:Section encode_js="1">$config->{now_available_text}</mt:Section>';
+if (locksmith_locked) {
+ TC.attachLoadEvent($func);
+} else {
+ TC.attachLoadEvent(startRenewLock);
+}
+</script>
+ };
+}
+
+sub param_edit_entry {
+ my ($cb, $app, $param) = @_;
+ return unless ($param->{id});
+ my $config = MT->component('locksmith')->get_config_hash('blog:' . $param->{blog_id});
+ _set_params($param, $config);
+ my $entry = MT->model('entry')->load($param->{'id'});
+ my $perms = $app->permissions;
+ if ($app->user->id != $entry->author_id) {
+ if ($perms->has('edit_all_posts_read_only')
+ && !$perms->has('administer_blog')) {
+ $param->{locksmith_locked} = 1;
+ $param->{locksmith_read_only} = 1;
+ } elsif ($config->{entry_locking} == 2) {
+ # entry author only
+ $param->{locksmith_locked} = 1;
+ $param->{locksmith_author_only} = 1;
+ my $author = MT->model('author')->load($entry->author_id);
+ $param->{entry_author_name} = $author->name;
+ $param->{entry_author_display_name} = $author->nickname;
+ if (_can_override($app, $config)) {
+ $param->{locksmith_override} = 1;
+ }
+ }
+ }
+ # only check for normal locking if not already locked based on author/perm
+ if (!$param->{locksmith_locked} && ($config->{entry_locking} == 1)) {
+ if (my $lock = MT->model('objectlock')->is_locked($entry)) {
+ $param->{locksmith_locked} = 1;
+ if (_can_override($app, $config)) {
+ $param->{locksmith_override} = 1;
+ }
+ } else {
+ my $lock = MT->model('objectlock')->set_lock($entry, $config->{hold_for});
+ $param->{locksmith_locked_until} = $lock->locked_until;
+ }
+ }
+}
+
+sub param_edit_template {
+ my ($cb, $app, $param) = @_;
+ return unless ($param->{id});
+ # need to account for global templates
+ my $scope = $param->{blog_id} ? "blog:$param->{blog_id}" : 'system';
+ my $config = MT->component('locksmith')->get_config_hash($scope);
+ return unless ($config->{template_locking});
+ my $template = MT->model('template')->load($param->{'id'});
+ if (my $lock = MT->model('objectlock')->is_locked($template)) {
+ $param->{locksmith_locked} = 1;
+ $param->{locksmith_locked_until} = $lock->locked_until;
+ if (_can_override($app, $config)) {
+ $param->{locksmith_override} = 1;
+ }
+ } else {
+ my $lock = MT->model('objectlock')->set_lock($template, $config->{hold_for});
+ $param->{locksmith_locked_until} = $lock->locked_until;
+ }
+ _set_params($param, $config);
+}
+
+sub source_edit_role {
+ my ($cb, $app, $template) = @_;
+ my $old = q{<mt:var name="prompt-edit_all_posts" escape="html"></label>};
+ my $new = q{
+<div id="locksmith-perms"><input type="checkbox" name="permission" value="edit_all_posts_read_only" id="permission-edit_all_posts_read_only"<mt:if name="have_access-edit_all_posts_read_only"> checked="checked"</mt:if> <mt:unless name="have_access-edit_all_posts"> disabled="disabled"</mt:unless> /> Read-Only&nbsp;&nbsp;
+</div>
+ };
+ $$template =~ s/\Q$old\E/$old$new/;
+ $old = q#function on_edit_all_posts_changed(obj) {#;
+ $new = <<HTML;
+$old
+ var eap = getByID('permission-edit_all_posts');
+ var eapro = getByID('permission-edit_all_posts_read_only');
+ if (eap && eap.checked) {
+ eapro.disabled = false;
+ if (eapro.checked) {
+ eape.disabled = false;
+ }
+ } else {
+ eapro.disabled = true;
+ eapro.checked = false;
+ }
+HTML
+ $$template =~ s/\Q$old\E/$new/;
+}
+
+sub _set_params {
+ my ($param, $config) = @_;
+ for my $key (qw( hold_for retry_every renew_every )) {
+ $param->{'locksmith_' . $key} = $config->{$key};
+ }
+ $param->{locksmith_locked_until} ||= 0;
+}
+
+sub renew_lock {
+ my ($app) = @_;
+ my ($user) = $app->login;
+ if (!$user) {
+ return $app->json_error('Your login has expired. Please save your changes and sign in again.');
+ }
+ return $app->json_error('No object ID') unless $app->param('id');
+ my $obj = MT->model($app->param('object_type') || 'entry')->load($app->param('id'));
+ return $app->json_error('Object not found') unless $obj;
+ my $config = MT->component('locksmith')->get_config_hash('blog:' . $obj->blog_id);
+ my $lock = MT->model('objectlock')->set_lock($obj, $config->{hold_for});
+ return $app->json_result({ result => $lock->locked_until });
+}
+
+sub retry_lock {
+ my ($app) = @_;
+ my ($user) = $app->login;
+ if (!$user) {
+ return $app->json_error('Your login has expired. Please sign in again.');
+ }
+ return $app->json_error('No object ID') unless $app->param('id');
+ my $obj = MT->model($app->param('object_type') || 'entry')->load($app->param('id'));
+ return $app->json_error('Object not found') unless $obj;
+ my $config = MT->component('locksmith')->get_config_hash('blog:' . $obj->blog_id);
+ if (my $lock = MT->model('objectlock')->is_locked($obj)) {
+ return $app->json_result({ result => 'Still locked' });
+ } else {
+ MT->model('objectlock')->set_lock($obj, $config->{hold_for});
+ return $app->json_result({ got_lock => 1 });
+ }
+}
+
+sub release_lock {
+ my ($app) = @_;
+ # errors and result won't really go anywhere, since this is called onunload
+ return unless $app->param('id');
+ my $obj = MT->model($app->param('object_type') || 'entry')->load($app->param('id'));
+ return unless $obj;
+ my $class = MT->model('objectlock');
+ my $lock = $class->current_lock($obj);
+ return unless $lock;
+ return unless ($lock->author_id == $app->user->id);
+ # if locked_until is different from what the browser has, that indicates that
+ # the lock has been updated, so we don't want to expire it; this prevents
+ # a race condition when the editing screen is reloaded
+ return unless ($lock->locked_until == $app->param('locked_until'));
+ $class->release_lock($obj);
+}
+
+sub _can_override {
+ my ($app, $config) = @_;
+ return 1 if ($app->user->is_superuser);
+ return 0 unless $config->{override_role};
+ my $role = MT->model('role')->load({ name => $config->{override_role} });
+ return 0 unless $role;
+ my %terms = (
+ role_id => $role->id,
+ author_id => $app->user->id,
+ blog_id => $app->param('blog_id') || 0
+ );
+ return MT->model('association')->count(\%terms);
+}
+
+1;
52 plugins/Locksmith/lib/Locksmith/Tags.pm
@@ -0,0 +1,52 @@
+
+package Locksmith::Tags;
+use strict;
+use Data::Dumper;
+
+sub hdlr_entry_locking_author {
+ my ($ctx, $args, $cond) = @_;
+ return hdlr_locking_author('entry', $ctx, $args, $cond);
+}
+
+sub hdlr_template_locking_author {
+ my ($ctx, $args, $cond) = @_;
+ return hdlr_locking_author('template', $ctx, $args, $cond);
+}
+
+sub hdlr_locking_author {
+ my ($object_type, $ctx, $args, $cond) = @_;
+ my $obj = MT->model($object_type)->load($ctx->var('id')) || return '';
+ my $lock = MT->model('objectlock')->is_locked($obj);
+ return '' unless $lock;
+ my $author = MT->model('author')->load($lock->author_id);
+ my $builder = $ctx->stash('builder');
+ my $tokens = $ctx->stash('tokens');
+ local $ctx->{__stash}{author} = $author;
+ defined(my $out = $builder->build($ctx, $tokens, $cond))
+ or return $ctx->error($builder->errstr);
+ return $out;
+}
+
+sub hdlr_entry_if_author_only {
+ my ($ctx, $args, $cond) = @_;
+ my $entry = MT->model('entry')->load($ctx->var('id')) || return 0;
+ my $config = MT->component('locksmith')->get_config_hash('blog:' . $ctx->stash('blog')->id);
+ return 0 unless ($config->{entry_locking} && ($config->{entry_locking} == 2));
+ return ($entry->author_id == MT->instance->user->id) ? 0 : 1;
+}
+
+sub hdlr_entry_if_read_only {
+ my ($ctx, $args, $cond) = @_;
+ my $entry = MT->model('entry')->load($ctx->var('id')) || return 0;
+ my $app = MT->instance;
+ my $perms = $app->permissions;
+ if ($app->user->id != $entry->author_id) {
+ if ($perms->has('edit_all_posts_read_only')
+ && !$perms->has('administer_blog')) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+1;
31 plugins/Locksmith/lib/Locksmith/Util.pm
@@ -0,0 +1,31 @@
+
+package Locksmith::Util;
+
+use MT;
+use Data::Dumper;
+$Data::Dumper::Maxdepth = 99;
+
+my $mt_apply_default_settings;
+
+sub init_app {
+ my ($app) = @_;
+
+ local $SIG{__WARN__} = sub {};
+ $mt_apply_default_settings = \&MT::Plugin::apply_default_settings;
+ *MT::Plugin::apply_default_settings = \&apply_default_settings;
+}
+
+sub apply_default_settings {
+ my ($plugin, $data, $scope_id) = @_;
+ return &{$mt_apply_default_settings}(@_) unless ($plugin->id eq 'locksmith');
+ return &{$mt_apply_default_settings}(@_) if ($scope_id eq 'system');
+ my $sys;
+ for my $key (keys %{$plugin->registry('settings')}) {
+ next if exists($data->{$key});
+ # don't load system settings unless we need to
+ $sys ||= $plugin->get_config_obj('system')->data;
+ $data->{$key} = $sys->{$key};
+ }
+}
+
+1;
83 plugins/Locksmith/lib/ObjectLock.pm
@@ -0,0 +1,83 @@
+
+package ObjectLock;
+use strict;
+use Data::Dumper;
+
+use base qw( MT::Object );
+
+__PACKAGE__->install_properties ({
+ column_defs => {
+ id => 'integer not null primary key auto_increment',
+ object_id => 'integer not null',
+ object_ds => 'string(20) not null',
+ author_id => 'integer not null',
+ locked_until => 'integer not null',
+ },
+ indexes => {
+ obj_id_ds => {
+ columns => [ 'object_id', 'object_ds' ],
+ },
+ author_id => 1,
+ },
+ audit => 1,
+ datasource => 'objectlock',
+ primary_key => 'id',
+});
+
+sub unixtime {
+ my $class = shift;
+ my $driver = $class->driver;
+ my $unixtime_sql = $driver->dbd->sql_for_unixtime;
+ return $driver->rw_handle->selectrow_array("SELECT $unixtime_sql");
+}
+
+sub is_locked {
+ my $class = shift;
+ my ($obj) = @_;
+ my $app = MT->instance;
+ my $unixtime = $class->unixtime;
+ my %terms = (
+ object_ds => $obj->datasource,
+ object_id => $obj->id,
+ locked_until => \">= $unixtime", #"
+ author_id => { not => $app->user->id },
+ );
+ return $class->load(\%terms);
+}
+
+sub current_lock {
+# return the current lock for an object, regardless of
+# whether it's expired or whether the logged-in user owns it
+ my $class = shift;
+ my ($obj) = @_;
+ my %terms = (
+ object_ds => $obj->datasource,
+ object_id => $obj->id,
+ );
+ return $class->load(\%terms);
+}
+
+sub set_lock {
+ my $class = shift;
+ my ($obj, $hold_for) = @_;
+ my %key_terms = (
+ object_ds => $obj->datasource,
+ object_id => $obj->id,
+ );
+ my %value_terms = (
+ locked_until => $class->unixtime + ($hold_for * 60),
+ author_id => MT->instance->user->id,
+ );
+ my $lock = $class->set_by_key(\%key_terms, \%value_terms);
+ $lock->save || die $lock->errstr;
+ return $lock;
+}
+
+sub release_lock {
+ my $class = shift;
+ my ($obj) = @_;
+ # passing 0 as the hold_for value will cause it to expire immediately
+ return $class->set_lock($obj, 0);
+}
+
+1;
106 plugins/Locksmith/tmpl/config.tmpl
@@ -0,0 +1,106 @@
+<mtapp:setting
+ id="entry_locking"
+ label="Entry Locking:"
+ >
+ <input type="radio" name="entry_locking" id="entry_locking_0" value="0"<mt:if name="entry_locking_0"> checked="checked"</mt:if> /> None&nbsp;
+ <input type="radio" name="entry_locking" id="entry_locking_1" value="1"<mt:if name="entry_locking_1"> checked="checked"</mt:if> /> Single Editor&nbsp;
+ <input type="radio" name="entry_locking" id="entry_locking_2" value="2"<mt:if name="entry_locking_2"> checked="checked"</mt:if> /> Entry's Author Only
+</mtapp:setting>
+
+<mtapp:setting
+ id="template_locking"
+ label="Template Locking:"
+ >
+ <input type="radio" name="template_locking" id="template_locking_0" value="0"<mt:if name="template_locking_0"> checked="checked"</mt:if> /> None&nbsp;
+ <input type="radio" name="template_locking" id="template_locking_1" value="1"<mt:if name="template_locking_1"> checked="checked"</mt:if> /> Single Editor&nbsp;
+</mtapp:setting>
+
+<mtapp:setting
+ id="hold_for"
+ label="Hold Lock For:"
+ hint="When editing an object with locking enabled"
+ show_hint="1"
+ >
+ <input type="text" name="hold_for" id="hold_for" value="<mt:var name="hold_for" escape="html">" class="quarter-width" /> minutes
+</mtapp:setting>
+
+<mtapp:setting
+ id="renew_every"
+ label="Renew Lock Every:"
+ hint="When editing an object with locking enabled"
+ show_hint="1"
+ >
+ <input type="text" name="renew_every" id="renew_every" value="<mt:var name="renew_every" escape="html">" class="quarter-width" /> minutes
+</mtapp:setting>
+
+<mtapp:setting
+ id="retry_every"
+ label="Retry Lock Every:"
+ hint="When viewing an object locked by another user"
+ show_hint="1"
+ >
+ <input type="text" name="retry_every" id="retry_every" value="<mt:var name="retry_every" escape="html">" class="quarter-width" /> minutes
+</mtapp:setting>
+
+<mtapp:setting
+ id="override_role"
+ label="Override Role:"
+ hint="Users with this role will be allowed to edit locked objects<br />(System Administrators can always override)"
+ show_hint="1"
+ >
+ <input type="text" name="override_role" id="override_role" value="<mt:var name="override_role" escape="html">" class="half-width" />
+</mtapp:setting>
+
+<mtapp:setting
+ id="locked_text"
+ label="'Locked' Message:"
+ hint="Text to be displayed when an object is locked; can include MT template code"
+ show_hint="1"
+ >
+ <input type="text" name="locked_text" id="locked_text" value="<mt:var name="locked_text" escape="html">" class="full-width" />
+</mtapp:setting>
+
+<mtapp:setting
+ id="author_only_text"
+ label="'Author Only' Message:"
+ hint="Text to be displayed when an object is locked because it can only be edited by its author"
+ show_hint="1"
+ >
+ <input type="text" name="author_only_text" id="author_only_text" value="<mt:var name="author_only_text" escape="html">" class="full-width" />
+</mtapp:setting>
+
+<mtapp:setting
+ id="override_text"
+ label="'Override?' Message:"
+ hint="Text of confirm dialog to override a lock"
+ show_hint="1"
+ >
+ <input type="text" name="override_text" id="override_text" value="<mt:var name="override_text" escape="html">" class="full-width" />
+</mtapp:setting>
+
+<mtapp:setting
+ id="override_author_only_text"
+ label="'Author Only Override?' Message:"
+ hint="Text of confirm dialog to allow editing of an entry that can only be edited by its author"
+ show_hint="1"
+ >
+ <input type="text" name="override_author_only_text" id="override_author_only_text" value="<mt:var name="override_author_only_text" escape="html">" class="full-width" />
+</mtapp:setting>
+
+<mtapp:setting
+ id="now_available_text"
+ label="'Now Available' Message:"
+ hint="Text of dialog alerting user when a locked entry is now available for editing"
+ show_hint="1"
+ >
+ <input type="text" name="now_available_text" id="now_available_text" value="<mt:var name="now_available_text" escape="html">" class="full-width" />
+</mtapp:setting>
+
+<mtapp:setting
+ id="read_only_text"
+ label="'Read-Only' Message:"
+ hint="Text to display when a user with Read-only access to an entry is viewing it"
+ show_hint="1"
+ >
+ <input type="text" name="read_only_text" id="read_only_text" value="<mt:var name="read_only_text" escape="html">" class="full-width" />
+</mtapp:setting>
Please sign in to comment.
Something went wrong with that request. Please try again.