matik12 / aurelia-permission

Aurelia plugin for UI user authorization using permission / roles
MIT License
21 stars 0 forks source link

Possibility to not render element at all #5

Open Robbe64 opened 6 years ago

Robbe64 commented 6 years ago

It would be nice to don't even render elements instead of hiding them. Is there a chance to get this feature?

schnetzi commented 6 years ago

I am also looking forward to get a feature like this. The easiest way would be to remove the element but not to render the element at all would be really nice.

schnetzi commented 6 years ago

Easy way but not nice way:

import { autoinject, customAttribute, bindable } from 'aurelia-framework';

import AuthorizationService from './authorization-service';

// Sample usage:
// global-permission-show="can: AddUsers"

@autoinject()
@customAttribute('global-permission-if')
export class GlobalPermissionIf {

  @bindable can = '';
  @bindable check = true;

  get canPermissions(): string[] {
    return this.can ? this.can.replace(' ', '').split(',') : [];
  }

  private element: any;

  constructor(element: Element, private authorizationService: AuthorizationService) {
    this.element = element;
  }

  bind() {
    if (this.check && !this.authorizationService.isAuthorizedByPermissionName(this.canPermissions)) {
      this.hideElement();
    }
  }

  private hideElement() {
    this.element.parentElement.removeChild(this.element);
  }
}