Skip to content
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

Fix: 404 on Hydra Oauth page #4835

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions imports/plugins/core/hydra-oauth/register.js
Expand Up @@ -12,8 +12,8 @@ Reaction.registerPackage({
autoEnable: true,
registry: [{
route: "/account/login",
name: "OAuth Login",
label: "oauth-login",
name: "account/login",
label: "OAuth Login",
meta: {
noAdminControls: true,
oauthLoginFlow: true
Expand Down
1 change: 1 addition & 0 deletions imports/plugins/core/hydra-oauth/server/index.js
@@ -1,5 +1,6 @@
import { Meteor } from "meteor/meteor";
import { oauthLogin } from "./oauthMethods";
import "./init";

Meteor.methods({
"oauth/login": oauthLogin
Expand Down
12 changes: 12 additions & 0 deletions imports/plugins/core/hydra-oauth/server/init.js
@@ -0,0 +1,12 @@
import Hooks from "@reactioncommerce/hooks";
import Reaction from "/imports/plugins/core/core/server/Reaction";

// Allow the login forms to be shown to all visitors.
// Without this, the route for the login page will not be published because of permission checks
Hooks.Events.add("afterCoreInit", () => {
Reaction.addRolesToGroups({
allShops: true,
groups: ["guest", "customer"],
roles: ["account/login"]
});
});
@@ -1,9 +1,6 @@
import _ from "lodash";
import Logger from "@reactioncommerce/logger";
import { Meteor } from "meteor/meteor";
import { Roles } from "meteor/alanning:roles";
import { Migrations } from "meteor/percolate:migrations";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import { Accounts, Groups, Shops } from "/lib/collections";

/**
Expand All @@ -16,9 +13,6 @@ Migrations.add({
version: 5,
up() {
const shops = Shops.find({}).fetch();

// needed to ensure restart in case of a migration that failed before finishing
Groups.remove({});
Accounts.update({}, { $set: { groups: [] } }, { bypassCollection2: true, multi: true });

if (shops && shops.length) {
Expand All @@ -34,12 +28,19 @@ Migrations.add({
permissionsArray.forEach((permissions, index) => {
if (!permissions) { return null; }
Logger.debug(`creating custom group for shop ${shop.name}`);
const groupId = Groups.insert({
// An "update" is preferred here to cover cases where a migration re-run is triggered (and thus avoids duplicates).
// (as against deleting all the documents before a re-run).
const groupId = Groups.update({
slug: `custom${index + 1}`
}, {
name: `custom ${index + 1}`,
slug: `custom${index + 1}`,
permissions,
shopId: shop._id
}, { bypassCollection2: true });
}, {
upsert: true,
bypassCollection2: true
});
updateAccountsInGroup({
shopId: shop._id,
permissions,
Expand All @@ -51,33 +52,18 @@ Migrations.add({

function createDefaultGroupsForShop(shop) {
let defaultGroupAccounts = [];
const { defaultRoles, defaultVisitorRole } = shop;
let ownerRoles = Roles.getAllRoles().fetch().map((role) => role.name);

ownerRoles = ownerRoles.concat(Reaction.defaultCustomerRoles);
ownerRoles = _.uniq(ownerRoles);
const groupNames = ["shop manager", "customer", "guest", "owner"];

const shopManagerRoles = ownerRoles.filter((role) => role !== "owner");
const roles = {
"shop manager": shopManagerRoles,
"customer": defaultRoles || Reaction.defaultCustomerRoles,
"guest": defaultVisitorRole || Reaction.defaultVisitorRoles,
"owner": ownerRoles
};

Object.keys(roles).forEach((groupKeys) => {
groupNames.forEach((groupKeys) => {
Logger.debug(`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}"`);
// On startup Reaction.init() creates the default groups, this finds existing groups
// and updates accounts that belong to them
const { _id, permissions } = Groups.findOne({ slug: groupKeys }) || {};
Logger.debug(`new group "${groupKeys}" created with id "${_id}"`);
const updatedAccounts = updateAccountsInGroup({
shopId: shop._id,
permissions: roles[groupKeys],
groupId
permissions,
_id
});
defaultGroupAccounts = defaultGroupAccounts.concat(updatedAccounts);
});
Expand All @@ -86,6 +72,7 @@ Migrations.add({

// finds all accounts with a permission set and assigns them to matching group
function updateAccountsInGroup({ shopId, permissions = [], groupId }) {
if (!groupId) return null;
impactmass marked this conversation as resolved.
Show resolved Hide resolved
const query = { [`roles.${shopId}`]: { $size: permissions.length, $all: permissions } };
const matchingUserIds = Meteor.users.find(query).fetch().map((user) => user._id);

Expand Down Expand Up @@ -125,7 +112,7 @@ Migrations.add({

/*
* helper func created to limit the permission sets available to unique values without duplicates.
* It takes a two dimentional array like this:
* It takes a two dimensional array like this:
* [
* ["tag", "product"],
* ["product", "tag"],
Expand Down