All files / app/service authz.service.ts

28.07% Statements 32/114
24.03% Branches 25/104
44.44% Functions 4/9
28.35% Lines 19/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106118x       118x           179x     179x 179x       2x                                                                       4x 3x 3x                                                 12x 11x 11x 11x 10x 9x 8x 7x 7x 5x                              
import { Injectable } from '@angular/core';
import { Ref } from '../model/ref';
import { Role } from '../model/user';
import { Store } from '../store/store';
import { capturesAny, hasTag, isOwner, isOwnerTag, localTag, privateTag, publicTag, qualifyTags } from '../util/tag';
import { ConfigService } from './config.service';
 
@Injectable({
  providedIn: 'root'
})
export class AuthzService {
 
  constructor(
    private store: Store,
    private config: ConfigService,
  ) { }
 
  writeAccess(ref: Ref): boolean {
    Eif (!this.store.account.signedIn) return false;
    if (ref.origin !== this.store.account.origin) return false;
    if (hasTag('locked', ref)) return false;
    if (this.store.account.mod) return true;
    if (isOwnerTag(this.store.account.tag, ref)) return true;
    if (!this.store.account.access) return false;
    if (isOwner(this.store.account.access, ref)) return true;
    return !!capturesAny(this.store.account.access.writeAccess, qualifyTags(ref.tags, ref.origin));
  }
 
  taggingAccess(ref: Ref): boolean {
    if (!this.store.account.signedIn) return false;
    if (ref.origin !== this.store.account.origin) return false;
    if (this.store.account.editor) return true;
    if (isOwnerTag(this.store.account.tag, ref)) return true;
    if (!this.store.account.access) return false;
    if (isOwner(this.store.account.access, ref)) return true;
    return !!capturesAny(this.store.account.access.writeAccess, qualifyTags(ref.tags, ref.origin));
  }
 
  deleteAccess(ref: Ref): boolean {
    if (!this.store.account.signedIn) return false;
    if (this.store.account.mod) return true;
    if (ref.origin !== this.store.account.origin) return false;
    return this.taggingAccess(ref);
  }
 
  queryReadAccess(query?: string): boolean {
    if (!query) return false;
    for (const part of query.split(/[-|:!()\s]+/)) {
      if (part && !this.tagReadAccess(part)) return false;
    }
    return true;
  }
 
  canAddTag(tag?: string): boolean {
    if (!tag) return false;
    tag = localTag(tag);
    Eif (publicTag(tag)) return true;
    if (!this.store.account.signedIn) return false;
    if (this.store.account.mod) return true;
    if (this.store.account.localTag === tag) return true;
    if (hasTag(tag, this.config.modSeals)) return false;
    if (!this.store.account.editor && hasTag(tag, this.config.editorSeals)) return false;
    if (!this.store.account.access) return false;
    if (capturesAny(this.store.account.access.tagReadAccess, [tag])) return true;
    return !!capturesAny(this.store.account.access.readAccess, [tag]);
  }
 
  tagReadAccess(tag?: string): boolean {
    if (!tag) return false;
    if (tag.startsWith('!')) tag = tag.substring(1);
    tag = localTag(tag);
    if (!privateTag(tag)) return true;
    if (!this.store.account.signedIn) return false;
    if (this.store.account.mod) return true;
    if (this.store.account.localTag === tag) return true;
    if (!this.store.account.access) return false;
    if (capturesAny(this.store.account.access.tagReadAccess, [tag])) return true;
    return !!capturesAny(this.store.account.access.readAccess, [tag]);
  }
 
  tagWriteAccess(tag?: string): boolean {
    if (!this.store.account.signedIn) return false;
    Iif (!tag) return false;
    tag = localTag(tag);
    if (tag === 'locked') return false;
    if (this.store.account.mod) return true;
    if (this.store.account.editor && publicTag(tag)) return true;
    if (this.store.account.localTag === tag) return true;
    Iif (!this.store.account.access) return false;
    if (capturesAny(this.store.account.access.tagWriteAccess, [tag])) return true;
    return !!capturesAny(this.store.account.access.writeAccess, [tag]);
  }
 
  hasRole(role: Role) {
    switch(role) {
      case 'ROLE_ADMIN': return this.store.account.admin;
      case 'ROLE_MOD': return this.store.account.mod;
      case 'ROLE_EDITOR': return this.store.account.editor;
      case 'ROLE_USER': return this.store.account.user;
      case 'ROLE_VIEWER': return this.store.account.viewer;
      case 'ROLE_ANONYMOUS': return true;
      case 'ROLE_BANNED': return this.store.account.banned;
    }
  }
}