Moving user functions to ohif:user package
This commit is contained in:
1 parent
b7d1e91848
commit
d2289f5eb0
7 files changed
+13
-19
No files matched your search
@@ -0,0 +1,34 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { OHIF } from 'meteor/ohif:core';
|
||||
|
||||
// Throw error if there is no user logged in
|
||||
OHIF.user.validate = () => {
|
||||
if (!Meteor.userId()) {
|
||||
throw new Meteor.Error('not-authorized');
|
||||
}
|
||||
};
|
||||
|
||||
// Get the persistent data by key
|
||||
OHIF.user.getData = key => {
|
||||
// Check if there is an user logged in
|
||||
OHIF.user.validate();
|
||||
|
||||
// Get user profile data
|
||||
const profile = Meteor.user().profile;
|
||||
|
||||
// Get the user persistent data
|
||||
const data = profile && profile.persistent;
|
||||
|
||||
if (data) {
|
||||
return data[key];
|
||||
}
|
||||
};
|
||||
|
||||
// Store the persistent data by giving a key and a value to store
|
||||
OHIF.user.setData = (key, value) => {
|
||||
// Check if there is an user logged in
|
||||
OHIF.user.validate();
|
||||
|
||||
// Call the update method on server-side
|
||||
Meteor.call('ohif.user.data.set', key, value);
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
import './data';
|
||||
import './getName';
|
||||
import './login';
|
||||
import './logout';
|
||||
|
||||
@@ -28,6 +28,9 @@ Package.onUse(function(api) {
|
||||
// Main module
|
||||
api.mainModule('main.js', ['client', 'server']);
|
||||
|
||||
// Server imports
|
||||
api.addFiles('server/index.js', 'server');
|
||||
|
||||
// Client imports
|
||||
api.addFiles('client/index.js', 'client');
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
import './user.js';
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { Accounts } from 'meteor/accounts-base';
|
||||
import { OHIF } from 'meteor/ohif:core';
|
||||
|
||||
// Manipulate user's persistent data
|
||||
class UserData {
|
||||
|
||||
// Get the persistent data by key
|
||||
static get(key) {
|
||||
// Check if there is an user logged in
|
||||
OHIF.MongoUtils.validateUser();
|
||||
|
||||
// Get the user's profile data
|
||||
const profileData = Meteor.user().profile;
|
||||
|
||||
// Return the data if it is set
|
||||
if (profileData.persistent) {
|
||||
return profileData.persistent[key];
|
||||
}
|
||||
}
|
||||
|
||||
// Store the persistent data by giving a key and a value to store
|
||||
static set(key, value) {
|
||||
// Check if there is an user logged in
|
||||
OHIF.MongoUtils.validateUser();
|
||||
|
||||
// Build the query to update only the current user's data
|
||||
const query = { _id: Meteor.userId() };
|
||||
|
||||
// Build the path to the persistent data with the given key
|
||||
const persistentPath = `profile.persistent.${key}`;
|
||||
|
||||
// Build the data to be updated
|
||||
const data = { $set: { [persistentPath]: value } };
|
||||
|
||||
// Update the user's profile data with the persistent information
|
||||
Accounts.users.update(query, data, OHIF.MongoUtils.writeCallback);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Register the methods
|
||||
Meteor.methods({
|
||||
'ohif.user.data.get': key => UserData.get(key),
|
||||
'ohif.user.data.set': (key, value) => UserData.set(key, value)
|
||||
});
|
||||
Reference in new issue
Block a user