From d2b0f3e09d70b5742afe45d5d080befae14ce40c Mon Sep 17 00:00:00 2001 From: Seun Martins Date: Sun, 30 Jul 2017 00:07:02 +0100 Subject: [PATCH] Add "Shop Manager" as a default group (#2602) * Setup up version for migrating from old permissions style to groups * Update accounts that match on migrate and add down method * Add shop manager as part of default groups on start and add to migration file * Show no dropdown if shop has only one group * If account with no name, use first part of email as name * Switch logger to debug * add sort to display higher permission groups at the top * Setup searching for and creating custom group and setting users to custom * Add comments to document flow of migration * Prevent error on user without roles field in tests * Change info logs to debug * Add comment --- .../accounts/client/components/groupsTableCell.js | 13 +++++++++++-- .../core/accounts/client/helpers/accountsHelper.js | 4 +++- .../migrations/5_update_defaultRoles_to_groups.js | 9 ++++++--- server/api/core/core.js | 9 ++++++--- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/imports/plugins/core/accounts/client/components/groupsTableCell.js b/imports/plugins/core/accounts/client/components/groupsTableCell.js index bfddc155a12..415e60904bc 100644 --- a/imports/plugins/core/accounts/client/components/groupsTableCell.js +++ b/imports/plugins/core/accounts/client/components/groupsTableCell.js @@ -5,11 +5,15 @@ import { Components, registerComponent } from "@reactioncommerce/reaction-compon import { getGravatar } from "../helpers/accountsHelper"; const GroupsTableCell = ({ account, columnName, group, groups, handleRemoveUserFromGroup, handleUserGroupChange }) => { + const email = _.get(account, "emails[0].address"); + if (columnName === "name") { + // use first part of email, if account has no name + const name = account.name || email.split("@")[0]; return (
- {account.name} + {name}
); } @@ -17,7 +21,7 @@ const GroupsTableCell = ({ account, columnName, group, groups, handleRemoveUserF if (columnName === "email") { return (
- {_.get(account, "emails[0].address")} + {email}
); } @@ -33,6 +37,11 @@ const GroupsTableCell = ({ account, columnName, group, groups, handleRemoveUserF } if (columnName === "dropdown") { + if (groups.length === 1) { + return ( +

{_.startCase(groups[0].name)}

+ ); + } const dropDownButton = (
diff --git a/imports/plugins/core/accounts/client/helpers/accountsHelper.js b/imports/plugins/core/accounts/client/helpers/accountsHelper.js index ea62f65bcce..7883bdaf7ca 100644 --- a/imports/plugins/core/accounts/client/helpers/accountsHelper.js +++ b/imports/plugins/core/accounts/client/helpers/accountsHelper.js @@ -11,7 +11,9 @@ import * as Collections from "/lib/collections"; * @return {Array} - array of groups, each having a `users` field */ export default function sortUsersIntoGroups({ accounts, groups }) { - const newGroups = groups.map((group) => { + // sort to display higher permission groups at the top + const sortedGroups = groups.sort((prev, next) => next.permissions.length - prev.permissions.length); + const newGroups = sortedGroups.map((group) => { const matchingAccounts = accounts.map((acc) => { if (acc.groups && acc.groups.indexOf(group._id) > -1) { return acc; diff --git a/imports/plugins/core/versions/server/migrations/5_update_defaultRoles_to_groups.js b/imports/plugins/core/versions/server/migrations/5_update_defaultRoles_to_groups.js index fcdbbfbbe8d..d21541fb6a8 100644 --- a/imports/plugins/core/versions/server/migrations/5_update_defaultRoles_to_groups.js +++ b/imports/plugins/core/versions/server/migrations/5_update_defaultRoles_to_groups.js @@ -47,10 +47,13 @@ Migrations.add({ function createDefaultGroupsForShop(shop) { let defaultGroupAccounts = []; const { defaultRoles, defaultVisitorRole } = shop; + const ownerRoles = Roles.getAllRoles().fetch().map(role => role.name); + const shopManagerRoles = ownerRoles.filter(role => role !== "owner"); const roles = { - customer: defaultRoles || [ "guest", "account/profile", "product", "tag", "index", "cart/checkout", "cart/completed"], - guest: defaultVisitorRole || ["anonymous", "guest", "product", "tag", "index", "cart/checkout", "cart/completed"], - owner: Roles.getAllRoles().fetch().map((role) => role.name) + "shop manager": shopManagerRoles, + "customer": defaultRoles || [ "guest", "account/profile", "product", "tag", "index", "cart/checkout", "cart/completed"], + "guest": defaultVisitorRole || ["anonymous", "guest", "product", "tag", "index", "cart/checkout", "cart/completed"], + "owner": ownerRoles }; Object.keys(roles).forEach((groupKeys) => { diff --git a/server/api/core/core.js b/server/api/core/core.js index 2f7d17b6f0f..b5eca6ca52a 100644 --- a/server/api/core/core.js +++ b/server/api/core/core.js @@ -63,10 +63,13 @@ export default { createDefaultGroups() { const allGroups = Groups.find({}).fetch(); const shops = Shops.find({}).fetch(); + const ownerRoles = Roles.getAllRoles().fetch().map(role => role.name); + const shopManagerRoles = ownerRoles.filter(role => role !== "owner"); const roles = { - customer: [ "guest", "account/profile", "product", "tag", "index", "cart/checkout", "cart/completed"], - guest: ["anonymous", "guest", "product", "tag", "index", "cart/checkout", "cart/completed"], - owner: Roles.getAllRoles().fetch().map(role => role.name) + "shop manager": shopManagerRoles, + "customer": [ "guest", "account/profile", "product", "tag", "index", "cart/checkout", "cart/completed"], + "guest": ["anonymous", "guest", "product", "tag", "index", "cart/checkout", "cart/completed"], + "owner": ownerRoles }; if (shops && shops.length) {