7798e9f9d3b5dcc30d3d02f4bb087bd624c6bba0
[tine20] / tine20 / Filemanager / js / GrantsPanel.js
1 /*
2  * Tine 2.0
3  *
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Cornelius WeiƟ <c.weiss@metaways.de>
6  * @copyright   Copyright (c) 2017 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8 Ext.ns('Tine.Filemanager');
9
10 Tine.Filemanager.GrantsPanel = Ext.extend(Ext.Panel, {
11
12     /**
13      * @cfg {Tine.widgets.dialog.EditDialog}
14      */
15     editDialog: null,
16
17     /**
18      * @cfg {Tine.Tinebase.Application} app
19      */
20     app: null,
21
22     /**
23      * @property {Tine.Filemanager.Model.Node} recordClass
24      */
25     recordClass: Tine.Filemanager.Model.Node,
26
27     requiredGrant: 'editGrant',
28     layout: 'fit',
29     border: false,
30
31     initComponent: function() {
32         this.app = this.app || Tine.Tinebase.appMgr.get('Filemanager');
33         this.title = this.title || this.app.i18n._('Grants');
34
35         this.editDialog.on('load', this.onRecordLoad, this);
36         this.editDialog.on('recordUpdate', this.onRecordUpdate, this);
37
38         this.hasOwnGrantsCheckbox = new Ext.form.Checkbox({
39             disabled: true,
40             boxLabel: this.app.i18n._('This folder has own grants'),
41             listeners: {scope: this, check: this.onOwnGrantsCheck}
42         });
43         this.hasOwnRightsDescription = new Ext.form.Label({
44             text: this.app.i18n._("Grants of a folder also apply recursively for all of its sub folders as long they don't have own grants itself.")
45         });
46         this.grantsGrid = new Tine.widgets.container.GrantsGrid({
47             downloadGrantTitle: 'Download', // i18n._('Download')
48             downloadGrantDescription: 'The grant to download files', // i18n._('The grant to download files')
49             publishGrantTitle: 'Publish', // i18n._('Publish')
50             publishGrantDescription: 'The grant to create anonymous download links for files', // i18n._('The grant to create anonymous download links for files')
51             grantContainer: 'Filemanager_Model_Node',
52             alwaysShowAdminGrant: true,
53             store: new Ext.data.JsonStore({
54                 fields: Tine.Tinebase.Model.Grant,
55                 root: 'grants'
56             }),
57             readOnly: true,
58             flex: 1
59         });
60
61         this.items = [{
62             layout: 'vbox',
63             align: 'stretch',
64             pack: 'start',
65             border: false,
66             items: [{
67                 layout: 'form',
68                 frame: true,
69                 hideLabels: true,
70                 width: '100%',
71                 items: [
72                     this.hasOwnGrantsCheckbox,
73                     this.hasOwnRightsDescription
74                 ]},
75                 this.grantsGrid
76             ]
77         }];
78
79         this.supr().initComponent.call(this);
80     },
81
82     onOwnGrantsCheck: function(cb, checked) {
83         this.grantsGrid.setReadOnly(!checked);
84     },
85
86     onRecordLoad: function(editDialog, record, ticketFn) {
87         var _ = window.lodash,
88             evalGrants = editDialog.evalGrants,
89             hasRequiredGrant = !evalGrants || _.get(record, record.constructor.getMeta('grantsPath') + '.' + this.requiredGrant);
90
91         this.hasOwnGrantsCheckbox.setDisabled(! lodash.get(record, 'data.account_grants.adminGrant', false)
92             || record.get('type') != 'folder');
93         this.hasOwnGrantsCheckbox.setValue(record.get('acl_node') == record.id);
94
95         this.grantsGrid.useGrant('admin', !!String(record.get('path')).match(/^\/shared/));
96         this.grantsGrid.getStore().loadData(record.data);
97
98         this.setReadOnly(! hasRequiredGrant);
99     },
100
101     setReadOnly: function(readOnly) {
102         this.readOnly = readOnly;
103         this.grantsGrid.setReadOnly(readOnly);
104         this.hasOwnGrantsCheckbox.setDisabled(readOnly);
105     },
106
107     onRecordUpdate: function(editDialog, record) {
108         var acl_node = this.hasOwnGrantsCheckbox.getValue() ? record.id : '',
109             grants = [];
110
111         this.grantsGrid.getStore().each(function(r) {grants.push(r.data)});
112
113         record.set('acl_node', acl_node);
114         record.set('grants', '');
115         record.set('grants', grants);
116     }
117 });