Skip to content

Commit

Permalink
Merge pull request #49 from fleetbase/dev-v0.2.15
Browse files Browse the repository at this point in the history
v0.2.15
  • Loading branch information
roncodes authored Aug 18, 2024
2 parents 1ed4989 + 4c57446 commit e875bf5
Show file tree
Hide file tree
Showing 13 changed files with 392 additions and 237 deletions.
39 changes: 39 additions & 0 deletions addon/abilities/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Ability } from 'ember-can';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { singularize } from 'ember-inflector';

export default class extends Ability {
@service currentUser;
@tracked service;
@tracked resource;
@tracked ability;
@tracked permissions = new Set();

constructor() {
super(...arguments);
this.permissions = new Set(this.currentUser.permissions.map((permission) => permission.name));
}

parseProperty(str) {
let [service, ability, resource] = str.split(' ');

this.service = service;
this.ability = ability;
this.resource = singularize(resource);

return 'can';
}

get can() {
if (this.currentUser.isAdmin) {
return true;
}

const permission = [this.service, this.ability, this.resource].join(' ');
const wilcardPermission = [this.service, '*', this.resource].join(' ');
const wildcardServicePermission = [this.service, '*'].join(' ');

return this.permissions.has(permission) || this.permissions.has(wilcardPermission) || this.permissions.has(wildcardServicePermission);
}
}
1 change: 1 addition & 0 deletions addon/exports/host-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const hostServices = [
'fileQueue',
'universe',
'intl',
'abilities',
{ hostRouter: 'router' },
];

Expand Down
1 change: 1 addition & 0 deletions addon/exports/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const services = [
'fileQueue',
'universe',
'intl',
'abilities',
];

export default services;
7 changes: 7 additions & 0 deletions addon/services/abilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Service from 'ember-can/services/abilities';

export default class AbilitiesService extends Service {
parse(propertyName) {
return { propertyName, abilityName: 'dynamic' };
}
}
151 changes: 100 additions & 51 deletions addon/services/current-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export default class CurrentUserService extends Service.extend(Evented) {
id: 'anon',
};

/**
* The current users permissions.
*
* @memberof CurrentUserService
*/
@tracked permissions = [];

/**
* User options in localStorage
*
Expand Down Expand Up @@ -96,36 +103,82 @@ export default class CurrentUserService extends Service.extend(Evented) {
*/
@alias('user.company_uuid') companyId;

/**
* Alias for if user is admin.
*
* @var {Boolean}
* @memberof CurrentUserService
*/
@alias('user.is_admin') isAdmin;

/**
* The prefix for this user options
*
* @var {String}
*/
@computed('id') get optionsPrefix() {
return `${this.id}:`;
}

get latitude() {
return this.whois('latitude');
}

get longitude() {
return this.whois('longitude');
}

get currency() {
return this.whois('currency.code');
}

get city() {
return this.whois('city');
}

get country() {
return this.whois('country_code');
}

/**
* Loads the current authenticated user
*
* @void
* @return Promise<UserModel>|null
*/
async load() {
if (this.session.isAuthenticated) {
let user = await this.store.findRecord('user', 'me');
this.set('user', user);
this.trigger('user.loaded', user);

// Set permissions
this.permissions = this.getUserPermissions(user);

return user;
}

return null;
}

/**
* Resolves a user model.
*
* @return {Promise}
* @return {Promise<User>}
*/
@action promiseUser(options = {}) {
const NoUserAuthenticatedError = new Error('Failed to authenticate user.');

return new Promise((resolve, reject) => {
if (this.session.isAuthenticated) {
return this.store
.queryRecord('user', { me: true })
.then((user) => {
try {
this.store.queryRecord('user', { me: true }).then((user) => {
// set the `current user`
this.set('user', user);
this.trigger('user.loaded', user);

// Set permissions
this.permissions = this.getUserPermissions(user);

// set environment from user option
this.theme.setEnvironment();

Expand All @@ -135,71 +188,67 @@ export default class CurrentUserService extends Service.extend(Evented) {
}

resolve(user);
})
.catch(() => {
reject(NoUserAuthenticatedError);
});
} catch (error) {
reject(NoUserAuthenticatedError);
}
} else {
reject(NoUserAuthenticatedError);
}
});
}

/**
* Loads and resolved all current users installed order configurations.
* Gets all user permissions.
*
* @return {Promise}
* @param {UserModel} user
* @return {Array}
* @memberof CurrentUserService
*/
@action getInstalledOrderConfigs(params = {}) {
return new Promise((resolve, reject) => {
this.fetch
.get('fleet-ops/order-configs/get-installed', params)
.then((configs) => {
const serialized = [];
getUserPermissions(user) {
const permissions = [];

for (let i = 0; i < configs.length; i++) {
const config = configs.objectAt(i);
const normalizedConfig = this.store.normalize('order-config', config);
const serializedConfig = this.store.push(normalizedConfig);
// get direct applied permissions
if (user.get('permissions')) {
permissions.pushObjects(user.get('permissions').toArray());
}

// get role permissions and role policies permissions
if (user.get('role')) {
if (user.get('role.permissions')) {
permissions.pushObjects(user.get('role.permissions').toArray());
}

serialized.pushObject(serializedConfig);
if (user.get('role.policies')) {
for (let i = 0; i < user.get('role.policies').length; i++) {
const policy = user.get('role.policies').objectAt(i);
if (policy.get('permissions')) {
permissions.pushObjects(policy.get('permissions').toArray());
}
}
}
}

resolve(serialized);
})
.catch(reject);
});
// get direct applied policy permissions
if (user.get('policies')) {
for (let i = 0; i < user.get('policies').length; i++) {
const policy = user.get('policies').objectAt(i);
if (policy.get('permissions')) {
permissions.pushObjects(policy.get('permissions').toArray());
}
}
}

return permissions;
}

/**
* The prefix for this user options
* Alias to get a user's whois property
*
* @var {String}
* @param {String} key
* @return {Mixed}
* @memberof CurrentUserService
*/
@computed('id') get optionsPrefix() {
return `${this.id}:`;
}

get latitude() {
return this.whois('latitude');
}

get longitude() {
return this.whois('longitude');
}

get currency() {
return this.whois('currency.code');
}

get city() {
return this.whois('city');
}

get country() {
return this.whois('country_code');
}

@action whois(key) {
return this.getWhoisProperty(key);
}
Expand Down
2 changes: 1 addition & 1 deletion addon/services/universe.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ export default class UniverseService extends Service.extend(Evented) {
// If component is a definition register to host application
if (typeof component === 'function') {
const owner = getOwner(this);
const widgetId = component.widgetId || widgetId || this._createUniqueWidgetHashFromDefinition(component);
widgetId = component.widgetId || widgetId || this._createUniqueWidgetHashFromDefinition(component);

if (owner) {
owner.register(`component:${widgetId}`, component);
Expand Down
1 change: 1 addition & 0 deletions app/abilities/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/abilities/dynamic';
1 change: 1 addition & 0 deletions app/services/abilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/services/abilities';
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
new Funnel(path.dirname(require.resolve('socketcluster-client')), {
files: ['socketcluster-client.min.js'],
destDir: '/assets',
allowEmpty: true,
})
);

Expand Down
Loading

0 comments on commit e875bf5

Please sign in to comment.