Skip to content

Commit

Permalink
Setup searching for and creating custom group and setting users to cu…
Browse files Browse the repository at this point in the history
…stom
  • Loading branch information
impactmass committed Jul 28, 2017
1 parent 9687910 commit 74dd3e9
Showing 1 changed file with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,35 @@ Migrations.add({
Groups.remove({});

if (shops && shops.length) {
shops.forEach((shop) => createGroupsForShop(shop));
shops.forEach((shop) => {
const defaultGroupAccounts = createDefaultGroupsForShop(shop);

const customPermissions = Meteor.users
.find({ _id: { $nin: defaultGroupAccounts } })
.fetch()
.map(user => user.roles[shop._id]);

const permissionsArray = sortUniqueArray(customPermissions);

permissionsArray.forEach((permissions, index) => {
Logger.info(`creating custom group for shop ${shop.name}`);
const groupId = Groups.insert({
name: `custom ${index + 1}`,
slug: `custom${index + 1}`,
permissions,
shopId: shop._id
});
updateAccountsInGroup({
shopId: shop._id,
permissions,
groupId
});
});
});
}

function createGroupsForShop(shop) {
function createDefaultGroupsForShop(shop) {
let defaultGroupAccounts = [];
const { defaultRoles, defaultVisitorRole } = shop;
const roles = {
customer: defaultRoles || [ "guest", "account/profile", "product", "tag", "index", "cart/checkout", "cart/completed"],
Expand All @@ -23,25 +48,37 @@ Migrations.add({
};

Object.keys(roles).forEach((groupKeys) => {
Logger.debug(`creating group ${groupKeys} for shop ${shop.name}`);
Logger.info(`creating group ${groupKeys} for shop ${shop.name}`);
const groupId = Groups.insert({
name: groupKeys,
slug: groupKeys,
permissions: roles[groupKeys],
shopId: shop._id
});
Logger.debug(`new group "${groupKeys}" created with id ${groupId}"`);
return updateAccountsBelongingToGroup({ shopId: shop._id, permissions: roles[groupKeys], groupId });
Logger.info(`new group "${groupKeys}" created with id "${groupId}"`);
const updatedAccounts = updateAccountsInGroup({
shopId: shop._id,
permissions: roles[groupKeys],
groupId
});
defaultGroupAccounts = defaultGroupAccounts.concat(updatedAccounts);
});
return defaultGroupAccounts;
}

function updateAccountsBelongingToGroup({ shopId, permissions, groupId }) {
const query = { [`roles.${shopId}`]: { $all: permissions } };
function updateAccountsInGroup({ shopId, permissions, groupId }) {
const query = { [`roles.${shopId}`]: { $size: permissions.length, $all: permissions } };
const matchingUserIds = Meteor.users.find(query).fetch().map((user) => user._id);

if (matchingUserIds.length) {
Logger.debug(`updating following matching Accounts to new group: ${matchingUserIds}`);
Logger.info(`updating following matching Accounts to new group: ${matchingUserIds}`);
}
Accounts.update({ _id: { $in: matchingUserIds }, shopId }, { $addToSet: { groups: groupId } });
Accounts.update(
{ _id: { $in: matchingUserIds }, shopId },
{ $addToSet: { groups: groupId } },
{ multi: true }
);
return matchingUserIds;
}
},
down() {
Expand All @@ -66,3 +103,19 @@ Migrations.add({
}
}
});

function sortUniqueArray(multiArray) {
const sorted = multiArray.map(x => {
if (!x) { return []; }
return x.sort();
}).sort();
const uniqueArray = [];
uniqueArray.push(sorted[0]);

for (let i = 1; i < sorted.length; i++) {
if (JSON.stringify(sorted[i]) !== JSON.stringify(sorted[i - 1])) {
uniqueArray.push(sorted[i]);
}
}
return uniqueArray;
}

0 comments on commit 74dd3e9

Please sign in to comment.