Forum Discussion

PerBonomi's avatar
9 years ago

Bulk remove roles

Hi all. I threw together a component that will let you, theoretically, remove a role from multiple users at the same time.

Caveats:

  1. It's not pretty
  2. It doesn't catch all (user) errors
  3. It's not localized
  4. It'll show a green confirmation, even if a user doesn't have the role (Sorry, no idea how to catch that without looping through all roles of every user; and I don't think all those extra api calls are necessary for this excercise.)
  5. In the first bit of javascript I'm unbinding "native" form submissions, because, for some reason, Lithium has a baked-in form submission protocol that hinders the custom form code from submitting.

Instructions:

  • Enter a role
  • enter users, separated by commas. E.g. user1,user2,user3
    I recommend not adding too many at the same time. It makes an api call for every user obviously
  • Submit

Good luck.

 

<#assign userIsAdmin = false/>
<#if !user.anonymous>
	<#list restadmin("/users/id/${user.id?c}/roles").roles.role as role>
		<#if role.name?? && role.name == "Administrator">
			<#assign userIsAdmin = true/>
			<#break>
		</#if>
	</#list>
</#if>
<#if userIsAdmin == false>
	Sorry, you're not an admin.
<#else>
	<#assign removeUsers = http.request.parameters.name.get("users", "") />
	<#assign removeRole = http.request.parameters.name.get("roleremove", "") />
	<#if removeUsers?? && removeUsers != "">
			<#assign sucUsers = []/>
			<#assign errUsers = []/>
			<#assign roles = restadmin("/roles").roles.role />
			<#list roles as role>
				<#if role.name == removeRole?trim>
					<#assign hrefRole = role.@href />
					<#break>
				</#if>
			</#list>
			<#if !hrefRole??>
				Role wasn't found
			<#else>
				<#list removeUsers?split(",") as u>
					<#assign result = restadmin("${hrefRole}/users/remove?role.user=/users/login/${u?trim}") />
					<#assign showResult = true/>
					<#if result?? && !result.error?has_content>
						<#assign sucUsers = sucUsers + ["${u}"]/>
					<#else>
						<#assign errUsers = errUsers + ["${u}"]/>
					</#if>
				</#list>
			</#if>
		</#if>
	<div id="BulkRemoveComponent" class="lia-panel lia-panel-standard lia-component-bulk-remove">
		<div class="lia-decoration-border">
			<div class="lia-decoration-border-top"><div></div></div>
			<div class="lia-decoration-border-content">
				<div>
					<div class="lia-panel-heading-bar-wrapper"><div class="lia-panel-heading-bar"><span class="lia-panel-heading-bar-title">Remove roles from users</span></div></div>
					<div class="lia-panel-content-wrapper">
						<div class="lia-panel-content">
							<div class="input-roles">
								<form id="test-form" action="/t5/custom/page/page-id/BulkRemoveUsers" method="post">
								<input id="role" type="text" name="roleremove" name="roleremove" placeholder="Role to remove"/>
								<textarea id="frm-name" type="text" name="users"></textarea>
								<input id="btSubmit" type="submit" value="Submit"/>
								</form>
							</div>
						</div>
						<#if showResult?? && showResult == true>
							<div id="FormResult" class="lia-quilt-column-alley lia-quilt-column-alley-single">
								<div class="InfoMessage lia-panel-feedback-banner-note lia-component-common-widget-page-feedback">
									<div id="FormResultSuc"><#list sucUsers as s>${s},</#list></div>
									<div id="FormResultErr"><#list errUsers as e>${e},</#list></div>
								</div>
							</div>
						</#if>
					</div>
				</div>
			</div>
		</div>
	</div>


	<script>
	$(document).ready(function () {
		$("#test-form").unbind("submit");
	});

		$("#test-form").submit(function(e) {
			e.preventDefault(); //STOP default action
			var postData = escape( $('#role').val().trim() + "," + $('#frm-name').val().trim() );
			var formURL = $(this).attr("action");
			$.ajax(
			{
				url : formURL,
				type: "POST",
				data &colon; postData,
				success:function(data, textStatus, jqXHR) 
				{
					
				},
				error: function(jqXHR, textStatus, errorThrown) 
				{
					$("#confirm").text("Error!");
				}
			});
		});				
	</script>
</#if>
No RepliesBe the first to reply