1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-25 08:09:43 +02:00

Set category permissions for both users and groups

This commit is contained in:
sauls8t 2018-03-06 10:39:47 +00:00
parent d1fdb385e9
commit 22679920d0
17 changed files with 335 additions and 124 deletions

View file

@ -10,14 +10,17 @@
// https://documize.com
import $ from 'jquery';
import Component from '@ember/component';
import { A } from "@ember/array"
import { inject as service } from '@ember/service';
import TooltipMixin from '../../mixins/tooltip';
import ModalMixin from '../../mixins/modal';
import Component from '@ember/component';
export default Component.extend(ModalMixin, TooltipMixin, {
userService: service('user'),
categoryService: service('category'),
// userService: service('user'),
spaceSvc: service('folder'),
groupSvc: service('group'),
categorySvc: service('category'),
appMeta: service(),
store: service(),
newCategory: '',
@ -42,43 +45,48 @@ export default Component.extend(ModalMixin, TooltipMixin, {
load() {
// get categories
this.get('categoryService').getAll(this.get('folder.id')).then((c) => {
this.get('categorySvc').getAll(this.get('folder.id')).then((c) => {
this.set('category', c);
// get summary of documents and users for each category in space
this.get('categoryService').getSummary(this.get('folder.id')).then((s) => {
this.get('categorySvc').getSummary(this.get('folder.id')).then((s) => {
c.forEach((cat) => {
let docs = _.findWhere(s, {categoryId: cat.get('id'), type: 'documents'});
let docCount = is.not.undefined(docs) ? docs.count : 0;
// let docs = _.findWhere(s, {categoryId: cat.get('id'), type: 'documents'});
// let docCount = is.not.undefined(docs) ? docs.count : 0;
let users = _.findWhere(s, {categoryId: cat.get('id'), type: 'users'});
let userCount = is.not.undefined(users) ? users.count : 0;
// let users = _.findWhere(s, {categoryId: cat.get('id'), type: 'users'});
// let userCount = is.not.undefined(users) ? users.count : 0;
let docs = _.where(s, {categoryId: cat.get('id'), type: 'documents'});
let docCount = 0;
docs.forEach((d) => { docCount = docCount + d.count });
let users = _.where(s, {categoryId: cat.get('id'), type: 'users'});
let userCount = 0;
users.forEach((u) => { userCount = userCount + u.count });
cat.set('documents', docCount);
cat.set('users', userCount);
});
});
// get users that this space admin user can see
this.get('userService').getSpaceUsers(this.get('folder.id')).then((users) => {
// set up Everyone user
let u = {
orgId: this.get('folder.orgId'),
folderId: this.get('folder.id'),
userId: '',
firstname: 'Everyone',
lastname: '',
};
let data = this.get('store').normalize('user', u)
users.pushObject(this.get('store').push(data));
users = users.sortBy('firstname', 'lastname');
this.set('users', users);
});
});
},
permissionRecord(who, whoId, name) {
let raw = {
id: whoId,
orgId: this.get('folder.orgId'),
categoryId: this.get('currentCategory.id'),
whoId: whoId,
who: who,
name: name,
categoryView: false,
};
let rec = this.get('store').normalize('category-permission', raw);
return this.get('store').push(rec);
},
setEdit(id, val) {
let cats = this.get('category');
let cat = cats.findBy('id', id);
@ -109,7 +117,7 @@ export default Component.extend(ModalMixin, TooltipMixin, {
folderId: this.get('folder.id')
};
this.get('categoryService').add(c).then(() => {
this.get('categorySvc').add(c).then(() => {
this.load();
});
},
@ -124,7 +132,7 @@ export default Component.extend(ModalMixin, TooltipMixin, {
onDelete() {
this.modalClose('#category-delete-modal');
this.get('categoryService').delete(this.get('deleteId')).then(() => {
this.get('categorySvc').delete(this.get('deleteId')).then(() => {
this.load();
});
},
@ -150,7 +158,7 @@ export default Component.extend(ModalMixin, TooltipMixin, {
cat = this.setEdit(id, false);
$('#edit-category-' + cat.get('id')).removeClass('is-invalid');
this.get('categoryService').save(cat).then(() => {
this.get('categorySvc').save(cat).then(() => {
this.load();
});
@ -160,45 +168,47 @@ export default Component.extend(ModalMixin, TooltipMixin, {
onShowAccessPicker(catId) {
this.set('showCategoryAccess', true);
let users = this.get('users');
let categoryPermissions = A([]);
let category = this.get('category').findBy('id', catId);
this.get('categoryService').getPermissions(category.get('id')).then((viewers) => {
// mark those users as selected that have already been given permission
// to see the current category;
users.forEach((user) => {
let userId = user.get('id');
let selected = viewers.isAny('whoId', userId);
user.set('selected', selected);
this.set('currentCategory', category);
this.set('categoryPermissions', categoryPermissions);
// get space permissions
this.get('spaceSvc').getPermissions(this.get('folder.id')).then((spacePermissions) => {
spacePermissions.forEach((sp) => {
let cp = this.permissionRecord(sp.get('who'), sp.get('whoId'), sp.get('name'));
cp.set('selected', false);
categoryPermissions.pushObject(cp);
});
this.set('categoryUsers', users);
this.set('currentCategory', category);
this.get('categorySvc').getPermissions(category.get('id')).then((perms) => {
// mark those users as selected that have permission to see the current category
perms.forEach((perm) => {
let c = categoryPermissions.findBy('whoId', perm.get('whoId'));
if (is.not.undefined(c)) {
c.set('selected', true);
}
console.log(perm.get('whoId'));
});
this.set('categoryPermissions', categoryPermissions.sortBy('who', 'name'));
});
});
},
onToggle(item) {
item.set('selected', !item.get('selected'));
},
onGrantAccess() {
this.set('showCategoryAccess', false);
let folder = this.get('folder');
let category = this.get('currentCategory');
let users = this.get('categoryUsers').filterBy('selected', true);
let viewers = [];
let perms = this.get('categoryPermissions').filterBy('selected', true);
users.forEach((user) => {
let userId = user.get('id');
let v = {
orgId: this.get('folder.orgId'),
folderId: this.get('folder.id'),
categoryId: category.get('id'),
userId: userId
};
viewers.push(v);
});
this.get('categoryService').setViewers(folder.get('id'), category.get('id'), viewers).then(() => {
this.get('categorySvc').setViewers(folder.get('id'), category.get('id'), perms).then(() => {
this.load();
});
}

View file

@ -0,0 +1,23 @@
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// 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
// by contacting <sales@documize.com>.
//
// https://documize.com
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
orgId: attr('string'),
categoryId: attr('string'),
whoId: attr('string'),
who: attr('string'),
categoryView: attr('boolean'),
name: attr('string'), // read-only
selected: attr('boolean') // client-side only
});

View file

@ -0,0 +1,13 @@
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
normalize(modelClass, resourceHash) {
return {
data: {
id: resourceHash.whoId ? resourceHash.whoId : 0,
type: modelClass.modelName,
attributes: resourceHash
}
};
}
});

View file

@ -87,7 +87,15 @@ export default BaseService.extend({
return this.get('ajax').request(`category/${categoryId}/permission`, {
method: 'GET'
}).then((response) => {
return response;
// return response;
let data = [];
data = response.map((obj) => {
let data = this.get('store').normalize('category-permission', obj);
return this.get('store').push(data);
});
return data;
});
},

View file

@ -6,7 +6,7 @@
<p class="sub-title">Sub-divide spaces into categories which can contain documents with restricted access.</p>
<form class="form-inline" onsubmit={{action 'onAdd'}}>
<div class="form-group mr-3">
{{input id="new-category-name" type='text' class="form-control mousetrap" placeholder="Category name" value=newCategory}}
{{focus-input id="new-category-name" type='text' class="form-control mousetrap" placeholder="Category name" value=newCategory}}
</div>
<button type="button" class="btn btn-success" onclick={{action 'onAdd'}}>Add</button>
</form>
@ -27,7 +27,7 @@
{{else}}
<div class="category col-8">
<div class="name">{{cat.category}}</div>
<div class="info">{{cat.documents}} {{if (eq cat.documents 1) 'document' 'documents' }}, {{cat.users}} {{if (eq cat.users 1) 'person' 'people' }}</div>
<div class="info">{{cat.documents}} {{if (eq cat.documents 1) 'document' 'documents' }} &middot; {{cat.users}} users/groups</div>
</div>
{{/if}}
<div class="col-4 buttons text-right">
@ -72,8 +72,40 @@
</div>
</div>
{{#ui/ui-dialog title="Set Cateogory Access" confirmCaption="Save" buttonType="btn-success" show=showCategoryAccess onAction=(action 'onGrantAccess')}}
{{#ui/ui-dialog title="Set Category Access" confirmCaption="Save" buttonType="btn-success" show=showCategoryAccess onAction=(action 'onGrantAccess')}}
<p>Select who can view documents within category</p>
{{ui/ui-list-picker items=categoryUsers nameField='fullname' singleSelect=false}}
<div class="widget-list-picker">
<ul class="options">
{{#each categoryPermissions as |permission|}}
<li class="option {{if permission.selected 'selected'}}" {{action 'onToggle' permission}}>
<div class="text text-truncate">
{{#if (eq permission.who "role")}}
<span class="button-icon-gray button-icon-small align-middle">
<i class="material-icons">people</i>
</span>
{{else}}
{{#if (eq permission.whoId constants.EveryoneUserId)}}
<span class="button-icon-gray button-icon-small align-middle">
<i class="material-icons">language</i>
</span>
{{else}}
<span class="button-icon-gray button-icon-small align-middle">
<i class="material-icons">person</i>
</span>
{{/if}}
{{/if}}
&nbsp;{{permission.name}}
{{#if (eq permission.whoId session.user.id)}}
<small class="form-text text-muted d-inline-block">(you)</small>
{{/if}}
</div>
{{#if permission.selected}}
<i class="material-icons">check</i>
{{/if}}
</li>
{{/each}}
</ul>
</div>
{{/ui/ui-dialog}}
</div>