bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/web/static/js/tokenNew.ts (about)

     1  /// <reference path="0-bosun.ts" />
     2  
     3  class NewTokenController {
     4      public token: Token = new Token();
     5      public permissions: Array<BitMeta>;
     6      public roles: Array<BitMeta>;
     7      public status: string;
     8  
     9      public createdToken: string;
    10  
    11      public hasBits = (bits: number) => {
    12          return (bits & this.token.Role) != 0;
    13      }
    14  
    15      public setRole = (bits: number, event: any) => {
    16          _(this.permissions).each((perm) => {
    17              if (!event.currentTarget.checked) {
    18                  perm.Active = false;
    19              } else {
    20                  perm.Active = (perm.Bits & bits) != 0;
    21              }
    22          });
    23      }
    24  
    25      public getBits = () => {
    26          return _(this.permissions).reduce((sum, p) => sum + (p.Active ? p.Bits : 0), 0)
    27      }
    28  
    29      public create() {
    30          this.token.Role = this.getBits();
    31          this.status = "Creating..."
    32  
    33          this.$http.post("/api/tokens", this.token).then(
    34              (resp: ng.IHttpPromiseCallbackArg<string>) => {
    35                  this.status = "";
    36                  this.createdToken = resp.data.replace(/"/g, "")
    37              },
    38              (err) => { this.status = 'Unable to load roles: ' + err; }
    39          )
    40      }
    41  
    42      public encoded() {
    43          return encodeURIComponent(this.createdToken)
    44      }
    45  
    46      static $inject = ['$http', 'authService'];
    47      constructor(private $http: ng.IHttpService, private auth: IAuthService) {
    48          var defs = auth.GetRoles();
    49          this.permissions = defs.Permissions;
    50          this.roles = defs.Roles;
    51      }
    52  }
    53  
    54  bosunApp.component("newToken", {
    55      controller: NewTokenController,
    56      controllerAs: "ct",
    57      templateUrl: "/partials/tokenNew.html"
    58  })