npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

user-grants

v1.0.0

Published

1. Go to https://gitlab.com/profile/keys and insert SSH key. If you don't have it, click on link "generate one" and paste the key on preview page 2. Open git bash and digit: ssh-keyscan -t rsa gitlab.com >> ~/.ssh/known_hosts

Downloads

4

Readme

HOW TO CONFIGURE INSTALL FROM GITLAB

  1. Go to https://gitlab.com/profile/keys and insert SSH key. If you don't have it, click on link "generate one" and paste the key on preview page
  2. Open git bash and digit: ssh-keyscan -t rsa gitlab.com >> ~/.ssh/known_hosts

HOW TO INSTALL APPLICATION

install user grants module

npm i -S git+ssh://[email protected]:abcdj/auth-client-frontend.git#0.0.1

import UserGrantsModule and Keycloak inside app.module.ts

import { initializer } from './_config/app-init';
import { KeycloakService, KeycloakAngularModule } from 'keycloak-angular';
import { UserGrantsModule } from "./node_modules/user-grants/user-grants.module";
import { UserGrantsService } from './node_modules/user-grants/src/app/services/user-grants.service';
...
@NgModule({
  imports: [
    KeycloakAngularModule,
    UserGrantsModule
    ],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: initializer,
      multi: true,
       deps: [KeycloakService, UserGrantsService]
    },
    KeycloakService
  ]
    
)}

inside app.init.ts import the service

import { KeycloakService } from 'keycloak-angular';
import { UserGrantsService } from '<root>/node_modules/user-grants/src/app/services/user-grants.service';

export function initializer(keycloak: KeycloakService, userGrants: UserGrantsService): () => Promise<any> {
  return (): Promise<any> => {
    return new Promise(async (resolve, reject) => {

        try {
          await keycloak.init(
            {
              config: {
                url: environment.keycloakRootUrl,
                realm: 'abcdj',
                clientId: 'mlm-ui-local'
              },
              initOptions: {
                onLoad: 'login-required',
                checkLoginIframe: false
              },
              bearerExcludedUrls: [ ]
            }
          );
          keycloak.getToken().then(token => {

            const grantJsonUrl = environment.endpoints.userCapabilities;

            localStorage.setItem('token', token);

            /* get all User grants */
            userGrants.init({
              urlJsonGrants: grantJsonUrl,
              token: environment.isKeycloakActivated ? localStorage.getItem('token') : null
            }).then(() => {
              resolve(token);
            }).catch(error => {
              reject(error);
            });

          }).catch(error => {
            reject(error);
          });

        } catch (error) {
          reject(error);
        }
    });
  };
}

create an auth.guard.ts file and add this content

import { Injectable } from '@angular/core';
import {
  Router,
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot
} from '@angular/router';
import { KeycloakService, KeycloakAuthGuard } from 'keycloak-angular';


@Injectable()
export class AuthGuard extends KeycloakAuthGuard implements CanActivate {

  cancelledNavigation: string;

  constructor(
    protected router: Router,
    protected keycloakAngular: KeycloakService) {
    super(router, keycloakAngular);
    this.cancelledNavigation = undefined;
  }

  isAccessAllowed(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {

      return new Promise(async (resolve, reject) => {

          if (!this.authenticated) {
            this.keycloakAngular.login();
            return;
          }
          const requiredRoles = route.data.roles;
          if (!requiredRoles || requiredRoles.length === 0) {
            return resolve(true);
          } else {
            if (!this.roles || this.roles.length === 0) {
              resolve(false);
            }
            let granted: boolean = false;
            for (const requiredRole of requiredRoles) {
              if (this.roles.indexOf(requiredRole) > -1) {
                granted = true;
                break;
              }
            }
            resolve(granted);
          }


      });

  }
}

in app.routes.ts add "canActivate: [AuthGuard]" for each route that require restricted access, as following

 { path: '', component: NameComponent, canActivate: [AuthGuard]}

to check permissions inside a component, import UserGrantService inside your component.ts and use the hasPermission('resource', 'scope') method in ts and html files, as following

import { UserGrantsService } from '../../../../node_modules/user-grants/src/app/services/user-grants.service';
 
constructor(public userGrants: UserGrantsService) {}

userGrants.hasPermission( 'profile', 'create');

[...]
 
 <li *ngIf="userGrants.hasPermission( 'profile', 'create')">