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"
2017-12-26 13:25:10 +00:00
import ModalMixin from '../../mixins/modal' ;
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' ) ,
appMeta : service ( ) ,
2016-08-23 10:13:29 +02:00
store : service ( ) ,
2018-03-03 17:46:29 +00:00
spacePermissions : null ,
2016-07-07 18:54:16 -07:00
2017-03-24 13:10:32 +00:00
didReceiveAttrs ( ) {
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' ) ) ;
spacePermissions . pushObject ( pr ) ;
2016-08-23 10:13:29 +02:00
} ) ;
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
spacePermissions . pushObject ( perm ) ;
2016-07-07 18:54:16 -07: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
} ) ;
2016-07-07 18:54:16 -07:00
} ,
2018-03-03 17:46:29 +00:00
permissionRecord ( who , whoId , name ) {
let raw = {
id : whoId ,
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 ,
} ;
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
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' ) ||
2017-12-24 15:51:43 +00:00
permission . get ( 'documentMove' ) || permission . get ( 'documentCopy' ) || permission . get ( 'documentTemplate' ) || permission . get ( 'documentApprove' ) ) ;
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' ) ||
2017-12-24 15:51:43 +00:00
permission . get ( 'documentMove' ) || permission . get ( 'documentCopy' ) || permission . get ( 'documentTemplate' ) || permission . get ( 'documentApprove' ) ) ) {
2017-10-09 10:56:59 -04:00
roleCount += 1 ;
}
} ) ;
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' ) ;
2017-11-24 12:50:06 +00:00
} ) ;
2016-07-07 18:54:16 -07:00
}
}
2016-07-24 14:49:40 -07:00
} ) ;