2016-07-07 18:54:16 -07:00
|
|
|
// 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
|
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
import { isEmpty } from '@ember/utils';
|
2016-07-07 18:54:16 -07:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
import RSVP from 'rsvp';
|
|
|
|
import Service, { inject as service } from '@ember/service';
|
2016-08-16 13:34:09 +02:00
|
|
|
|
2017-11-16 13:28:05 +00:00
|
|
|
export default Service.extend({
|
2016-08-16 13:34:09 +02:00
|
|
|
sessionService: service('session'),
|
|
|
|
ajax: service(),
|
|
|
|
store: service(),
|
2016-07-26 09:22:50 -07:00
|
|
|
|
|
|
|
// Adds a new user.
|
|
|
|
add(user) {
|
|
|
|
return this.get('ajax').request(`users`, {
|
|
|
|
type: 'POST',
|
|
|
|
data: JSON.stringify(user),
|
|
|
|
contentType: 'json'
|
2016-08-08 18:39:28 +02:00
|
|
|
}).then((response) => {
|
2016-08-16 13:34:09 +02:00
|
|
|
let data = this.get('store').normalize('user', response);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-26 09:22:50 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-01 10:58:55 +00:00
|
|
|
// Adds comma-delim list of users (firstname, lastname, email).
|
|
|
|
addBulk(list) {
|
|
|
|
return this.get('ajax').request(`users/import`, {
|
|
|
|
type: 'POST',
|
|
|
|
data: list,
|
|
|
|
contentType: 'text'
|
|
|
|
}).then(() => {
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-07-26 09:22:50 -07:00
|
|
|
// Returns user model for specified user id.
|
|
|
|
getUser(userId) {
|
|
|
|
let url = `users/${userId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
type: 'GET'
|
|
|
|
}).then((response) => {
|
2016-08-16 13:34:09 +02:00
|
|
|
let data = this.get('store').normalize('user', response);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-26 09:22:50 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-03-22 12:04:48 +00:00
|
|
|
// Returns all active users for organization.
|
2016-07-26 09:22:50 -07:00
|
|
|
getAll() {
|
2017-03-22 12:04:48 +00:00
|
|
|
return this.get('ajax').request(`users?active=1`).then((response) => {
|
2016-08-08 18:39:28 +02:00
|
|
|
return response.map((obj) => {
|
2016-08-16 13:34:09 +02:00
|
|
|
let data = this.get('store').normalize('user', obj);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-26 09:22:50 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-03-22 12:04:48 +00:00
|
|
|
// Returns all active and inactive users for organization.
|
|
|
|
getComplete() {
|
|
|
|
return this.get('ajax').request(`users?active=0`).then((response) => {
|
|
|
|
return response.map((obj) => {
|
|
|
|
let data = this.get('store').normalize('user', obj);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-09-22 10:22:52 +01:00
|
|
|
// Returns all users that can see space.
|
|
|
|
getSpaceUsers(spaceId) {
|
|
|
|
let url = `users/space/${spaceId}`;
|
2016-07-26 09:22:50 -07:00
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: "GET"
|
|
|
|
}).then((response) => {
|
|
|
|
let data = [];
|
2016-08-12 14:02:57 +02:00
|
|
|
|
|
|
|
data = response.map((obj) => {
|
|
|
|
let data = this.get('store').normalize('user', obj);
|
2016-08-26 13:13:26 +02:00
|
|
|
return this.get('store').push(data);
|
2016-07-26 09:22:50 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Updates an existing user record.
|
|
|
|
save(user) {
|
2016-08-24 13:10:54 +02:00
|
|
|
let userId = user.id;
|
2016-07-26 09:22:50 -07:00
|
|
|
let url = `users/${userId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
type: 'PUT',
|
|
|
|
data: JSON.stringify(user),
|
|
|
|
contentType: 'json'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// updatePassword changes the password for the specified user.
|
|
|
|
updatePassword(userId, password) {
|
|
|
|
let url = `users/${userId}/password`;
|
|
|
|
|
|
|
|
return this.get('ajax').post(url, {
|
|
|
|
data: password
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Removes the specified user.
|
|
|
|
remove(userId) {
|
|
|
|
let url = `users/${userId}`;
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'DELETE'
|
2016-08-08 18:39:28 +02:00
|
|
|
}).then(() => {
|
|
|
|
let user = this.get('store').peekRecord('user', `${userId}`);
|
|
|
|
return user.deleteRecord();
|
2016-07-26 09:22:50 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Request password reset.
|
|
|
|
forgotPassword(email) {
|
|
|
|
let url = `public/forgot`;
|
|
|
|
|
2016-08-16 13:34:09 +02:00
|
|
|
if (isEmpty(email)) {
|
|
|
|
return RSVP.reject("invalid");
|
2016-07-26 09:22:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let data = JSON.stringify({
|
|
|
|
email: email
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Set new password.
|
|
|
|
resetPassword(token, password) {
|
|
|
|
var url = `public/reset/${token}`;
|
|
|
|
|
2016-08-16 13:34:09 +02:00
|
|
|
if (isEmpty(token) || isEmpty(password)) {
|
|
|
|
return RSVP.reject("invalid");
|
2016-07-26 09:22:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.get('ajax').request(url, {
|
|
|
|
method: "POST",
|
|
|
|
data: password
|
|
|
|
});
|
2018-02-28 14:55:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// matchUsers on firstname, lastname, email
|
|
|
|
matchUsers(text) {
|
|
|
|
return this.get('ajax').request('users/match', {
|
|
|
|
method: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
contentType: 'text',
|
|
|
|
data: text
|
|
|
|
}).then((response) => {
|
|
|
|
let data = [];
|
|
|
|
|
|
|
|
data = response.map((obj) => {
|
|
|
|
let data = this.get('store').normalize('user', obj);
|
|
|
|
return this.get('store').push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
}
|
2016-07-26 14:52:32 +02:00
|
|
|
});
|