bitwarden-estensione-browser/src/app/organizations/vault/add-edit.component.ts

87 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-07-05 15:42:50 +02:00
import {
Component,
OnInit,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { ApiService } from 'jslib/abstractions/api.service';
import { AuditService } from 'jslib/abstractions/audit.service';
import { CipherService } from 'jslib/abstractions/cipher.service';
import { FolderService } from 'jslib/abstractions/folder.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-07-18 15:21:23 +02:00
import { MessagingService } from 'jslib/abstractions/messaging.service';
2018-07-05 15:42:50 +02:00
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { StateService } from 'jslib/abstractions/state.service';
import { TokenService } from 'jslib/abstractions/token.service';
import { TotpService } from 'jslib/abstractions/totp.service';
import { CipherData } from 'jslib/models/data/cipherData';
import { Cipher } from 'jslib/models/domain/cipher';
import { Organization } from 'jslib/models/domain/organization';
import { CipherRequest } from 'jslib/models/request/cipherRequest';
2018-07-05 19:14:33 +02:00
import { AddEditComponent as BaseAddEditComponent } from '../../vault/add-edit.component';
2018-07-05 15:42:50 +02:00
@Component({
selector: 'app-org-vault-add-edit',
2018-07-05 19:14:33 +02:00
templateUrl: '../../vault/add-edit.component.html',
2018-07-05 15:42:50 +02:00
})
export class AddEditComponent extends BaseAddEditComponent implements OnInit {
organization: Organization;
originalCipher: Cipher = null;
2018-07-05 15:42:50 +02:00
constructor(cipherService: CipherService, folderService: FolderService,
i18nService: I18nService, platformUtilsService: PlatformUtilsService,
analytics: Angulartics2, toasterService: ToasterService,
auditService: AuditService, stateService: StateService,
tokenService: TokenService, totpService: TotpService,
2018-07-18 15:21:23 +02:00
passwordGenerationService: PasswordGenerationService, private apiService: ApiService,
messagingService: MessagingService) {
2018-07-05 15:42:50 +02:00
super(cipherService, folderService, i18nService, platformUtilsService, analytics,
2018-07-18 15:21:23 +02:00
toasterService, auditService, stateService, tokenService, totpService, passwordGenerationService,
messagingService);
2018-07-05 15:42:50 +02:00
}
protected async loadCipher() {
2018-07-05 16:48:51 +02:00
if (!this.organization.isAdmin) {
2018-07-05 15:42:50 +02:00
return await super.loadCipher();
}
2018-07-05 16:48:51 +02:00
const response = await this.apiService.getCipherAdmin(this.cipherId);
const data = new CipherData(response);
this.originalCipher = new Cipher(data);
return new Cipher(data);
2018-07-05 15:42:50 +02:00
}
2018-07-05 16:10:30 +02:00
protected encryptCipher() {
if (!this.editMode) {
this.cipher.organizationId = this.organization.id;
}
if (!this.organization.isAdmin) {
return super.encryptCipher();
}
return this.cipherService.encrypt(this.cipher, null, this.originalCipher);
2018-07-05 16:10:30 +02:00
}
2018-07-05 15:42:50 +02:00
protected async saveCipher(cipher: Cipher) {
2018-07-05 16:48:51 +02:00
if (!this.organization.isAdmin) {
2018-07-05 15:42:50 +02:00
return super.saveCipher(cipher);
}
2018-07-05 16:48:51 +02:00
const request = new CipherRequest(cipher);
if (this.editMode) {
return this.apiService.putCipherAdmin(this.cipherId, request);
} else {
return this.apiService.postCipherAdmin(request);
}
2018-07-05 15:42:50 +02:00
}
protected async deleteCipher() {
2018-07-05 16:48:51 +02:00
if (!this.organization.isAdmin) {
2018-07-05 15:42:50 +02:00
return super.deleteCipher();
}
2018-07-05 16:48:51 +02:00
return this.apiService.deleteCipherAdmin(this.cipherId);
2018-07-05 15:42:50 +02:00
}
}