2016-07-07 18:54:16 -07:00
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
2016-08-17 15:37:46 +02:00
// This software (Documize Community Edition) is licensed under
2016-07-07 18:54:16 -07:00
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
2016-08-17 15:37:46 +02:00
// by contacting <sales@documize.com>.
2016-07-07 18:54:16 -07:00
//
// https://documize.com
2017-11-16 13:28:05 +00:00
import { inject as service } from '@ember/service' ;
2018-03-03 17:46:29 +00:00
import { A } from "@ember/array"
2018-03-05 12:47:42 +00:00
import { debounce } from '@ember/runloop' ;
2017-12-26 13:25:10 +00:00
import ModalMixin from '../../mixins/modal' ;
2018-04-26 12:03:23 +01:00
import stringUtil from '../../utils/string' ;
2018-03-03 17:46:29 +00:00
import Component from '@ember/component' ;
2016-08-17 15:37:46 +02:00
2017-12-26 13:25:10 +00:00
export default Component . extend ( ModalMixin , {
2018-03-03 17:46:29 +00:00
groupSvc : service ( 'group' ) ,
spaceSvc : service ( 'folder' ) ,
userSvc : service ( 'user' ) ,
2018-03-27 11:33:43 +01:00
router : service ( ) ,
2018-03-14 13:38:37 +00:00
appMeta : service ( ) ,
2016-08-23 10:13:29 +02:00
store : service ( ) ,
2018-03-03 17:46:29 +00:00
spacePermissions : null ,
2018-03-05 12:47:42 +00:00
users : null ,
searchText : '' ,
2018-03-14 13:38:37 +00:00
2017-03-24 13:10:32 +00:00
didReceiveAttrs ( ) {
2018-03-27 11:33:43 +01:00
this . _super ( ... arguments ) ;
2018-03-03 17:46:29 +00:00
let spacePermissions = A ( [ ] ) ;
let constants = this . get ( 'constants' ) ;
2016-07-07 18:54:16 -07:00
2018-03-03 17:46:29 +00:00
// get groups
this . get ( 'groupSvc' ) . getAll ( ) . then ( ( groups ) => {
this . set ( 'groups' , groups ) ;
2016-08-23 10:13:29 +02:00
2018-03-03 17:46:29 +00:00
groups . forEach ( ( g ) => {
let pr = this . permissionRecord ( constants . WhoType . Group , g . get ( 'id' ) , g . get ( 'name' ) ) ;
2018-03-05 12:47:42 +00:00
pr . set ( 'members' , g . get ( 'members' ) ) ;
2018-03-03 17:46:29 +00:00
spacePermissions . pushObject ( pr ) ;
2016-08-23 10:13:29 +02:00
} ) ;
2018-03-05 12:47:42 +00:00
let hasEveryoneId = false ;
2018-03-03 17:46:29 +00:00
// get space permissions
this . get ( 'spaceSvc' ) . getPermissions ( this . get ( 'folder.id' ) ) . then ( ( permissions ) => {
permissions . forEach ( ( perm , index ) => { // eslint-disable-line no-unused-vars
// is this permission for group or user?
if ( perm . get ( 'who' ) === constants . WhoType . Group ) {
// group permission
spacePermissions . forEach ( ( sp ) => {
if ( sp . get ( 'whoId' ) == perm . get ( 'whoId' ) ) {
sp . setProperties ( perm ) ;
}
} ) ;
} else {
// user permission
2018-03-05 12:47:42 +00:00
if ( perm . get ( 'whoId' ) === constants . EveryoneUserId ) {
perm . set ( 'name' , ' ' + perm . get ( 'name' ) ) ;
hasEveryoneId = true ;
}
2018-03-03 17:46:29 +00:00
spacePermissions . pushObject ( perm ) ;
2016-07-07 18:54:16 -07:00
}
} ) ;
2018-03-05 12:47:42 +00:00
// always show everyone
if ( ! hasEveryoneId ) {
let pr = this . permissionRecord ( constants . WhoType . User , constants . EveryoneUserId , ' ' + constants . EveryoneUserName ) ;
2018-03-14 13:38:37 +00:00
spacePermissions . pushObject ( pr ) ;
2018-03-05 12:47:42 +00:00
}
2018-03-03 17:46:29 +00:00
this . set ( 'spacePermissions' , spacePermissions . sortBy ( 'who' , 'name' ) ) ;
2016-07-07 18:54:16 -07:00
} ) ;
2017-08-07 14:42:02 +01:00
} ) ;
2018-03-05 12:47:42 +00:00
this . set ( 'searchText' , '' ) ;
2016-07-07 18:54:16 -07:00
} ,
2018-03-03 17:46:29 +00:00
permissionRecord ( who , whoId , name ) {
let raw = {
2018-04-26 12:03:23 +01:00
id : stringUtil . makeId ( 16 ) ,
2018-03-03 17:46:29 +00:00
orgId : this . get ( 'folder.orgId' ) ,
folderId : this . get ( 'folder.id' ) ,
whoId : whoId ,
who : who ,
name : name ,
spaceView : false ,
spaceManage : false ,
spaceOwner : false ,
documentAdd : false ,
documentEdit : false ,
documentDelete : false ,
documentMove : false ,
documentCopy : false ,
documentTemplate : false ,
documentApprove : false ,
2018-03-14 13:38:37 +00:00
documentLifecycle : false ,
documentVersion : false ,
2018-03-03 17:46:29 +00:00
} ;
let rec = this . get ( 'store' ) . normalize ( 'space-permission' , raw ) ;
return this . get ( 'store' ) . push ( rec ) ;
} ,
2017-03-24 13:10:32 +00:00
getDefaultInvitationMessage ( ) {
2017-09-15 11:08:05 +01:00
return "Hey there, I am sharing the " + this . get ( 'folder.name' ) + " space (in " + this . get ( "appMeta.title" ) + ") with you so we can both collaborate on documents." ;
2017-03-24 13:10:32 +00:00
} ,
2016-07-07 18:54:16 -07:00
2018-03-05 12:47:42 +00:00
matchUsers ( s ) {
let spacePermissions = this . get ( 'spacePermissions' ) ;
let filteredUsers = A ( [ ] ) ;
this . get ( 'userSvc' ) . matchUsers ( s ) . then ( ( users ) => {
users . forEach ( ( user ) => {
let exists = spacePermissions . findBy ( 'whoId' , user . get ( 'id' ) ) ;
if ( is . undefined ( exists ) ) {
filteredUsers . pushObject ( user ) ;
}
} ) ;
this . set ( 'filteredUsers' , filteredUsers ) ;
} ) ;
} ,
2017-03-24 13:10:32 +00:00
actions : {
setPermissions ( ) {
let message = this . getDefaultInvitationMessage ( ) ;
2018-03-03 17:46:29 +00:00
let permissions = this . get ( 'spacePermissions' ) ;
2017-09-15 11:08:05 +01:00
let folder = this . get ( 'folder' ) ;
let payload = { Message : message , Permissions : permissions } ;
2018-03-03 17:46:29 +00:00
let constants = this . get ( 'constants' ) ;
2017-08-07 14:42:02 +01:00
2018-03-03 17:46:29 +00:00
let hasEveryone = _ . find ( permissions , ( permission ) => {
return permission . get ( 'whoId' ) === constants . EveryoneUserId &&
2017-09-15 11:08:05 +01:00
( permission . get ( 'spaceView' ) || permission . get ( 'documentAdd' ) || permission . get ( 'documentEdit' ) || permission . get ( 'documentDelete' ) ||
2018-03-14 13:38:37 +00:00
permission . get ( 'documentMove' ) || permission . get ( 'documentCopy' ) || permission . get ( 'documentTemplate' ) ||
permission . get ( 'documentApprove' ) || permission . get ( 'documentLifecycle' ) || permission . get ( 'documentVersion' ) ) ;
2016-07-07 18:54:16 -07:00
} ) ;
2017-03-24 13:10:32 +00:00
2017-10-09 10:56:59 -04:00
// see if more than oen user is granted access to space (excluding everyone)
let roleCount = 0 ;
permissions . forEach ( ( permission ) => {
2018-03-03 17:46:29 +00:00
if ( permission . get ( 'whoId' ) !== constants . EveryoneUserId &&
2017-10-09 10:56:59 -04:00
( permission . get ( 'spaceView' ) || permission . get ( 'documentAdd' ) || permission . get ( 'documentEdit' ) || permission . get ( 'documentDelete' ) ||
2018-03-14 13:38:37 +00:00
permission . get ( 'documentMove' ) || permission . get ( 'documentCopy' ) || permission . get ( 'documentTemplate' ) ||
permission . get ( 'documentApprove' ) || permission . get ( 'documentLifecycle' ) || permission . get ( 'documentVersion' ) ) ) {
roleCount += 1 ;
2017-10-09 10:56:59 -04:00
}
} ) ;
2017-09-15 11:08:05 +01:00
if ( is . not . undefined ( hasEveryone ) ) {
folder . markAsPublic ( ) ;
} else {
2017-10-09 10:56:59 -04:00
if ( roleCount > 1 ) {
2017-09-15 11:08:05 +01:00
folder . markAsRestricted ( ) ;
} else {
folder . markAsPrivate ( ) ;
}
}
2017-11-24 12:50:06 +00:00
2018-03-03 17:46:29 +00:00
this . get ( 'spaceSvc' ) . savePermissions ( folder . get ( 'id' ) , payload ) . then ( ( ) => {
2017-12-26 13:25:10 +00:00
this . modalClose ( '#space-permission-modal' ) ;
2018-03-27 11:33:43 +01:00
this . get ( 'onRefresh' ) ( ) ;
2017-11-24 12:50:06 +00:00
} ) ;
2018-03-05 12:47:42 +00:00
} ,
onSearch ( ) {
2018-03-14 13:38:37 +00:00
debounce ( this , function ( ) {
2018-03-05 12:47:42 +00:00
let searchText = this . get ( 'searchText' ) . trim ( ) ;
if ( searchText . length === 0 ) {
this . set ( 'filteredUsers' , A ( [ ] ) ) ;
return ;
}
this . matchUsers ( searchText ) ;
} , 250 ) ;
} ,
onAdd ( user ) {
let spacePermissions = this . get ( 'spacePermissions' ) ;
let constants = this . get ( 'constants' ) ;
let exists = spacePermissions . findBy ( 'whoId' , user . get ( 'id' ) ) ;
if ( is . undefined ( exists ) ) {
spacePermissions . pushObject ( this . permissionRecord ( constants . WhoType . User , user . get ( 'id' ) , user . get ( 'fullname' ) ) ) ;
this . set ( 'spacePermissions' , spacePermissions ) ;
}
} ,
2016-07-07 18:54:16 -07:00
}
2016-07-24 14:49:40 -07:00
} ) ;