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

     1  /// <reference path="0-bosun.ts" />
     2  
     3  class AuthService implements IAuthService {
     4      private roles: RoleDefs;
     5      private userPerms: number;
     6      private username: string;
     7      private authEnabled: boolean;
     8      public Init(authEnabled: boolean, username: string, roles: RoleDefs, userPerms: number) {
     9          this.roles = roles;
    10          this.username = username;
    11          this.userPerms = userPerms;
    12          this.authEnabled = authEnabled;
    13          this.cleanRoles();
    14          if (!authEnabled) {
    15              var cookVal = readCookie("action-user")
    16              if (cookVal) {
    17                  this.username = cookVal;
    18              }
    19          }
    20      }
    21  
    22      public HasPermission(s: string) {
    23          for (let p of this.roles.Permissions) {
    24              if (p.Name == s) {
    25                  return (p.Bits & this.userPerms) != 0
    26              }
    27          }
    28          return true;
    29      }
    30  
    31      public PermissionsFor(bits: number): string[] {
    32          if (bits == null) {
    33              bits = this.userPerms;
    34          }
    35          var perms = [];
    36          for (let p of this.roles.Permissions) {
    37              if (p.Bits & bits) {
    38                  perms.push(p.Name);
    39              }
    40          }
    41          return perms;
    42      }
    43  
    44      public RoleFor(bits: number): string {
    45          if (bits == null) {
    46              bits = this.userPerms;
    47          }
    48          var perms = [];
    49           for (let r of this.roles.Roles){
    50                if (r.Bits == bits) {
    51                   return r.Name;
    52               }
    53          }
    54          return null;
    55      }
    56  
    57      public GetRoles() {
    58          return this.roles;
    59      }
    60      public Username(u: string) {
    61          if (!this.authEnabled && angular.isDefined(u)) {
    62              this.username = u;
    63              createCookie("action-user", u, 90);
    64          }
    65          return this.username
    66      }
    67      public GetUsername(): string {
    68          return this.username
    69      }
    70      public Enabled() {
    71          return this.authEnabled;
    72      }
    73      private cleanRoles() {
    74          //fix admin role that has extra bits corresponding to future permissions.
    75          //causes bit math to go crazy and overflow. 
    76          //prevents easily  making tokens that grant unknown future perms too.
    77          _(this.roles.Roles).each((role) => {
    78              var mask = 0;
    79              _(this.roles.Permissions).each((p) => {
    80                  if ((p.Bits & role.Bits) != 0) {
    81                      mask |= p.Bits
    82                  }
    83              })
    84              role.Bits = mask;
    85          })
    86      }
    87  }
    88  bosunApp.service("authService", AuthService)
    89  
    90  //simple component to show a <username-input> easily
    91  class UsernameInputController {
    92      static $inject = ['authService'];
    93      constructor(private auth: IAuthService) {
    94      }
    95  }
    96  bosunApp.component("usernameInput", {
    97      controller: UsernameInputController,
    98      controllerAs: "ct",
    99      template: '<input type="text"class="form-control"  ng-disabled="ct.auth.Enabled()" ng-model="ct.auth.Username" ng-model-options="{ getterSetter: true }">',
   100  })