-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat): remove ability for non-marketplace owner to switch shops #3375
(feat): remove ability for non-marketplace owner to switch shops #3375
Conversation
@spencern this works as-is in this PR, however, please see comment on the original ticket (#3184 (comment)) for perhaps an update we can make to make it a little better. |
@kieckhafer left some comments over there on #3184 - we can pair on this if you like. What I think we need to make 100% certain of in this PR is that on login a user's activeShop is set correctly, and on logout it's set back to the primary shop correctly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works really well, but I'd like to change the method used in getShopsForUser as reduce
would be a better fit than find
for this use case.
I've also committed a change to use the same getShopsForUser
to determine which shops should be shown in the shop selector if the user is an admin for multiple.
client/modules/core/main.js
Outdated
const shops = []; | ||
const user = Meteor.user(userId); | ||
|
||
Object.keys(user.roles).find((shopId) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this works, it's not really how find
is designed to work and may be confusing. Array.prototype.find
should return a value (the first value that returns true from the provided callback function). reduce
would be a better choice here as it's designed to loop through an array and return a single value (array, or otherwise) derived from the original input.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
e.g. something like this
const user = Meteor.user(userId);
const shopIds = Object.keys(user.roles);
// Reduce shopIds to shopsWithPermission
const shopIdsWithRoles = shopIds.reduce((shopsWithPermission, shopId) => {
// Get list of roles user has for this shop
const rolesUserHas = user.roles[shopId];
// Find first role that is included in the passed in roles array, otherwise hasRole is undefined
const hasRole = rolesUserHas.find((roleUserHas) => roles.includes(roleUserHas));
// if we found the role, then the user has permission for this shop. Add shopId to shopsWithPermission array
if (hasRole) {
shopsWithPermission.push(shopId);
}
return shopsWithPermission;
}, []);
return shopIdsWithRoles;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I know we discussed reduce
earlier, but once coding, I saw this find
inside the hasDashboardAccessForAnyShop
, and figured I'd keep the code as similar as I could to other code we'd written.
); | ||
}); | ||
|
||
// If user account has Marketplace permissions, show shop switcher |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment could better reflect the new idea that we show it when a user has admin/dashboard permission for multiple shops
{menuItems} | ||
</DropDownMenu> | ||
); | ||
// If the user is just a shop owner, not a marketplace owner, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update this comment for user with admin/dashboard permissions in multiple shops
@spencern updated comments, and to use reduce. One last thing I was noticing... Marketplace owners will always have the switcher, even if there is only one shop, because of the Should we account for this, or is this OK to always show for the main owner? |
@kieckhafer good catch, we should probably exclude the |
@spencern updated to remove I made the check on line 324 by just checking to see if Is it better practice to do the filtering of the
|
…thub.com/reactioncommerce/reaction into kieckhafer-feat-3184-shopSwitcherUpdates
@kieckhafer yeah, that would be better because it would not loop through all roles unnecessarily for the |
@spencern cool, updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. 👍
…hopSwitcherUpdates (feat): remove ability for non-marketplace owner to switch shops
This update changes the shop selector dropdown to only allow marketplace owners to switch shops. If you are a single shop owner, the selector will no longer be displayed, and you will no longer be allowed to switch active shops.
To test:
THEN: