import { PersonEntry , NodeEntry , PeopleApi } from '@alfresco/js-api' ; import { PersonModel , SitesApi , UploadApi , NodesApi , FavoritesApi , SearchApi , NodeContentTree , Person , SharedLinksApi } from './repo-client/apis' ; import { UserActions } from './user-actions' ; import { browser } from 'protractor' ; export class AdminActions extends UserActions { constructor ( ) { super ( ) ; } sites : SitesApi = new SitesApi ( ) ; upload : UploadApi = new UploadApi ( ) ; nodes : NodesApi = new NodesApi ( ) ; favorites : FavoritesApi = new FavoritesApi ( ) ; search : SearchApi = new SearchApi ( ) ; shared : SharedLinksApi = new SharedLinksApi ( ) ; async login ( username ? , password ? ) { return super . login ( username || browser . params . ADMIN_USERNAME , password || browser . params . ADMIN_PASSWORD ) ; } async getDataDictionaryId ( ) { try { return this . nodes . getNodeIdFromParent ( 'Data Dictionary' , '-root-' ) ; } catch ( error ) { super . handleError ( 'Admin Actions - getDataDictionaryId failed : ' , error ) ; return '' ; } } async getNodeTemplatesFolderId ( ) { try { return this . nodes . getNodeIdFromParent ( 'Node Templates' , await this . getDataDictionaryId ( ) ) ; } catch ( error ) { super . handleError ( 'Admin Actions - getNodeTemplatesFolderId failed : ' , error ) ; return '' ; } } async getSpaceTemplatesFolderId ( ) { try { return this . nodes . getNodeIdFromParent ( 'Space Templates' , await this . getDataDictionaryId ( ) ) ; } catch ( error ) { super . handleError ( 'Admin Actions - getSpaceTemplatesFolderId failed : ' , error ) ; return '' ; } } async createUser ( user ) { const person = new Person ( user ) ; const peopleApi = new PeopleApi ( this . alfrescoApi ) ; await this . login ( ) ; try { return peopleApi . createPerson ( person ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createUser failed : ' , error ) ; return null ; } } async disableUser ( username ) { const peopleApi = new PeopleApi ( this . alfrescoApi ) ; await this . login ( ) ; try { return peopleApi . updatePerson ( username , { enabled : false } ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createUser failed : ' , error ) ; return null ; } } async changePassword ( username , newPassword ) { const peopleApi = new PeopleApi ( this . alfrescoApi ) ; await this . login ( ) ; try { return peopleApi . updatePerson ( username , { password : newPassword } ) ; } catch ( error ) { super . handleError ( 'Admin Actions - changePassword failed : ' , error ) ; return null ; } } async createNodeTemplate ( name , title = '' , description = '' , author = '' ) { try { const templatesRootFolderId = await this . getNodeTemplatesFolderId ( ) ; return this . nodes . createFile ( name , templatesRootFolderId , title , description , author ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createNodeTemplate failed : ' , error ) ; return null ; } } async createNodeTemplatesHierarchy ( hierarchy ) { try { return this . nodes . createContent ( hierarchy , `Data Dictionary/Node Templates` ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createNodeTemplatesHierarchy failed : ' , error ) ; } } async createSpaceTemplate ( name , title = '' , description = '' ) { try { const templatesRootFolderId = await this . getSpaceTemplatesFolderId ( ) ; return this . nodes . createFolder ( name , templatesRootFolderId , title , description ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createSpaceTemplate failed : ' , error ) ; return null ; } } async createSpaceTemplatesHierarchy ( hierarchy ) { try { return this . nodes . createContent ( hierarchy , `Data Dictionary/Space Templates` ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createSpaceTemplatesHierarchy failed : ' , error ) ; } } async removeUserAccessOnNodeTemplate ( nodeName ) { try { const templatesRootFolderId = await this . getNodeTemplatesFolderId ( ) ; const nodeId = await this . nodes . getNodeIdFromParent ( nodeName , templatesRootFolderId ) ; return this . nodes . setInheritPermissions ( nodeId , false ) ; } catch ( error ) { super . handleError ( 'Admin Actions - removeUserAccessOnNodeTemplate failed : ' , error ) ; return null ; } } async removeUserAccessOnSpaceTemplate ( nodeName ) { try { const templatesRootFolderId = await this . getSpaceTemplatesFolderId ( ) ; const nodeId = await this . nodes . getNodeIdFromParent ( nodeName , templatesRootFolderId ) ; return this . nodes . setInheritPermissions ( nodeId , false ) ; } catch ( error ) { super . handleError ( 'Admin Actions - removeUserAccessOnSpaceTemplate failed : ' , error ) ; return null ; } } async cleanupNodeTemplatesFolder ( ) { try { return this . nodes . deleteNodeChildren ( await this . getNodeTemplatesFolderId ( ) ) ; } catch ( error ) { super . handleError ( 'Admin Actions - cleanupNodeTemplatesFolder failed : ' , error ) ; } } async cleanupNodeTemplatesItems ( nodeNames ) { try { const templatesFolderId = await this . getNodeTemplatesFolderId ( ) ; for ( const nodeName of nodeNames ) { const nodeId = await this . nodes . getNodeIdFromParent ( nodeName , templatesFolderId ) ; await this . nodes . deleteNodeById ( nodeId ) ; } } catch ( error ) { super . handleError ( 'Admin Actions - cleanupNodeTemplatesItems failed : ' , error ) ; } } async cleanupSpaceTemplatesFolder ( ) { try { const spaceTemplatesNodeId = await this . getSpaceTemplatesFolderId ( ) ; const nodesToDelete = ( await this . nodes . getNodeChildren ( spaceTemplatesNodeId ) ) . list . entries . filter ( ( node ) => node . entry . nodeType !== 'app:folderlink' && node . entry . name !== 'Software Engineering Project' ) . map ( ( node ) => node . entry . id ) ; return this . nodes . deleteNodesById ( nodesToDelete ) ; } catch ( error ) { super . handleError ( 'Admin Actions - cleanupSpaceTemplatesFolder failed : ' , error ) ; } } async cleanupSpaceTemplatesItems ( nodeNames ) { try { const spaceTemplatesNodeId = await this . getSpaceTemplatesFolderId ( ) ; for ( const nodeName of nodeNames ) { const nodeId = await this . nodes . getNodeIdFromParent ( nodeName , spaceTemplatesNodeId ) ; await this . nodes . deleteNodeById ( nodeId ) ; } } catch ( error ) { super . handleError ( 'Admin Actions - cleanupSpaceTemplatesFolder failed : ' , error ) ; } } async createLinkToFileId ( originalFileId , destinationParentId ) { try { return this . nodes . createFileLink ( originalFileId , destinationParentId ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createLinkToFileId failed : ' , error ) ; return null ; } } async createLinkToFileName ( originalFileName , originalFileParentId , destinationParentId ? ) { if ( ! destinationParentId ) { destinationParentId = originalFileParentId ; } try { const nodeId = await this . nodes . getNodeIdFromParent ( originalFileName , originalFileParentId ) ; return this . createLinkToFileId ( nodeId , destinationParentId ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createLinkToFileName failed : ' , error ) ; return null ; } } async createLinkToFolderId ( originalFolderId , destinationParentId ) { try { return this . nodes . createFolderLink ( originalFolderId , destinationParentId ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createLinkToFolderId failed : ' , error ) ; return null ; } } async createLinkToFolderName ( originalFolderName , originalFolderParentId , destinationParentId ? ) { if ( ! destinationParentId ) { destinationParentId = originalFolderParentId ; } try { const nodeId = await this . nodes . getNodeIdFromParent ( originalFolderName , originalFolderParentId ) ; return this . createLinkToFolderId ( nodeId , destinationParentId ) ; } catch ( error ) { super . handleError ( 'Admin Actions - createLinkToFolderName failed : ' , error ) ; return null ; } } }