Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-02-23 20:44:56

visualpeople
Member
From: Corvallis, Oregon - USA
Registered: 2005-11-16
Posts: 73
Website

Restrict a user group to load images in a specific category

I’m not sure this is even possible, but I want to restrict a user group — say “Staff Writers” to just be able to upload images to the image category “Staff Writer Images”. Any ideas?

A little background:

I’m using smd_user_manager and esq_autoimageresize to manage a staff list where users can log in and update info about themselves — including uploading a picture.

Everyone who has access is a trusted person, but they are more academic-types who are unconcerned with following specific upload rules and/or resizing their photos before uploading them. So I am using esq_autoimageresize to limit the size of photo uploaded to 250px wide for the category of “Staff Writer Images” But I want to guarantee that they won’t be able to upload their images into some other category, or no category at all and bypass my restrictions.

Right now I’m just using the awesome SLIR to hard-code a reduced-size image in my article template, which isn’t a horrible solution, but it would be great to be able to not have to worry about 2MB images clogging up the server when we go to search for other non-bio photos.

Any advice is appreciated!

-Ryan

Offline

#2 2012-02-23 21:17:44

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Restrict a user group to load images in a specific category

If your categories change quite often you could name that special category in a way that it always stays on top or at the bottom of the menu, hide each menu item via CSS and make only the upload category menu item visible again.
With the help of bot_admin_body_class you can then apply the rules only for the desired group of users.
EDIT: Nope, won’t work. I continue thinking ;)

Last edited by uli (2012-02-23 21:22:23)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#3 2012-02-23 22:20:40

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Restrict a user group to load images in a specific category

Had some fun while thinking and rediscovered nice snippets, but dash against the step of an additional saving that can easily be avoided. Sorry.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#4 2012-02-23 23:20:10

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Restrict a user group to load images in a specific category

If you want it securely you would have to rewrite the whole uploader. Or well, you could technically enforce it by modifying POST/GET data directly too, but yeah.

But is it possible at all? Yes. You will have to relay some JavaScript (or output buffer modifying), tho for hiding the select field on the editor pane. The nice part, that makes this even possible, is the upload form’s “hidden” feature; categories. The upload can actually take some meta data. Textpattern doesn’t offer GUI option for that, but it’s there.

During uploading it’s possible to set a category either by modifying POST/GET data on the correct event, or adding a hidden field to the form. The snippet below is going to do the latter using pluggable_ui. Then what is left is checking the user’s privileges level, and hiding the select field on the editor form with some JavaScript.

/**
 * Hook abc_restrict_imgcat() to callback event "image_ui", "upload_form",
 * and abc_restrict_imgcat_hide to "admin_side", "head_end".
 */

	register_callback('abc_restrict_imgcat', 'image_ui', 'upload_form');
	register_callback('abc_restrict_imgcat_hide', 'admin_side','head_end'); 

/**
 * Add a category field to the upload form
 * @see $txp_user, fetch()
 */

	function abc_restrict_imgcat($event, $step, $form, $args) {
		global $txp_user;

		/*
			See if the user is part of the Staff Writes (#4) group, and that the
			form is for "image_insert" step. If not, end here.
		*/

		if(fetch('privs', 'txp_users', 'name', $txp_user) != 4 || $args[2] != 'image_insert')
			return;

		/*
			The category's name, e.g. staff-writer-images
		*/

		$category = 'staff-writer-images';

		/*
			Add "category" named input to the upload form
		*/

		return str_replace('</form>', hInput('category', $category).'</form>', $form);
	}

/**
 * Hide category field from the editor panel with JS. Adds JS to <head>
 * @see $event, $txp_user, script_js(), fetch()
 */

	function abc_restrict_imgcat_hide() {

		global $event, $txp_user;

		/*
			Check that event is image, and that the user is staff writer
		*/

		if($event != 'image' || fetch('privs', 'txp_users', 'name', $txp_user) != 4)
			return;

		/*
			Convert the select field to hidden input
		*/

		$js = <<<EOF
			$(document).ready(function() {
				var c = $('.edit-pane select#image-category').val();
				$('.edit-pane p.category').after('<input type="hidden" name="category" value="'+c+'" />');
				$('.edit-pane p.category').remove();
			});
EOF;

		echo script_js($js);
	}

On the above script the staff-writer-images is the name of the category. The code is commented, and outlines what it does. I tested it briefly, so it should be good to go.

The snippet can be loaded as a plugin. It can be done by saving the code as abc_restrict_imgcat.v0.1.php to plugin cache directory, or alternatively by saving it as a admin-side plugin using ied_plugin_composer.

Last edited by Gocom (2012-02-23 23:32:50)

Offline

#5 2012-02-24 00:34:43

visualpeople
Member
From: Corvallis, Oregon - USA
Registered: 2005-11-16
Posts: 73
Website

Re: Restrict a user group to load images in a specific category

Wow, amazing Jukka! Thanks. I’ll give that a try and see if I can cobble together a plugin…

Also Uli thanks for pointing me in the direction of bot_admin_body_class — I had missed that one and it actually solves another problem I was having by letting me keep some other stuff hidden!

Offline

Board footer

Powered by FluxBB