add mocked auth

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-10-15 01:24:12 +03:00
committed by Daniel Hutzel
parent 937d9caf2b
commit 49f6b8c060
30 changed files with 151 additions and 271 deletions

View File

@@ -1,10 +1,17 @@
using {sap.capire.media.store as my} from '../db/schema';
@(requires : ['identified-user'])
service BrowseTracks {
@readonly
entity Tracks as projection on my.Tracks;
entity Tracks as projection on my.Tracks excluding {
alreadyOrdered
};
@(requires : 'authenticated-user')
@readonly
entity MarkedTracks as projection on my.Tracks;
@readonly
entity Genres as projection on my.Genres;
entity Genres as projection on my.Genres {
* , tracks : redirected to Tracks
};
}

View File

@@ -1,5 +1,14 @@
const cds = require("@sap/cds");
// only for demo cds.run(string, args)
const SELECT_INVOICES_BY_EMAIL = `
select invoice.ID
from sap_capire_media_store_Invoices invoice
join sap_capire_media_store_Customers customer
on customer.ID = invoice.customer_ID
where customer.email=?
`;
module.exports = async function () {
const db = await cds.connect.to("db"); // connect to database service
const { Invoices } = db.entities;
@@ -15,20 +24,21 @@ module.exports = async function () {
);
});
this.on("READ", "Tracks", async (req, next) => {
if (!!req._query && "my" in req._query) {
const myTrackEntries = await db.run(
this.on("READ", "MarkedTracks", async (req) => {
const myTrackIds = (
await db.run(
SELECT.from(Invoices)
.columns("ID")
.where({ customer_ID: req.user.attr.ID })
);
const myTrackIdsSequence = myTrackEntries.map(({ ID }) => ID).join();
const condition = cds.parse.expr(`ID in (${myTrackIdsSequence})`);
const query = SELECT.from(req.query).where(condition);
const result = await db.run(query);
result.$count = result.length;
return result;
}
return next();
)
).map(({ ID }) => ID);
const result = await db.run(req.query);
return result.map((columns) => {
return {
...columns,
alreadyOrdered: myTrackIds.includes(columns.ID),
};
});
});
};

View File

@@ -0,0 +1,12 @@
using {sap.capire.media.store as my} from '../db/schema';
@(requires : 'authenticated-user')
service InvoicesService {
@readonly
entity Invoices as projection on my.Invoices actions {
action invoice();
}
@readonly
entity InvoiceItems as projection on my.InvoiceItems;
}

View File

@@ -0,0 +1,20 @@
const cds = require("@sap/cds");
module.exports = async function () {
const db = await cds.connect.to("db"); // connect to database service
this.before("*", (req) => {
console.log(
"[USER]:",
req.user.id,
" [LEVEL]: ",
req.user.attr.level,
"[ROLE]",
req.user.is("user") ? "user" : "other"
);
});
this.on("READ", "Invoices", async (req) => {
return await db.run(req.query.where({ customer_ID: req.user.attr.ID }));
});
};

View File

@@ -11,10 +11,9 @@ service MediaService {
entity MediaTypes as projection on my.MediaTypes;
entity PlaylistTrack as projection on my.PlaylistTrack;
entity Playlists as projection on my.Playlists;
@(restrict : [{
grant : '*',
where : '$user.level > 1'
}])
// @(restrict : [{
// grant : '*',
// where : '$user.level > 1'
// }])
entity Tracks as projection on my.Tracks;
}

View File

@@ -0,0 +1,11 @@
using {sap.capire.media.store as my} from '../db/schema';
service UserService {
function mockLogin(email : String(111), password : String(200)) returns {
roles : array of String(111);
level : Integer;
mockedToken : String(500);
email : my.Persone.email;
ID : my.Persone.ID
}
}

View File

@@ -0,0 +1,39 @@
const cds = require("@sap/cds");
module.exports = async function () {
const db = await cds.connect.to("db"); // connect to database service
const { Employees, Customers } = db.entities;
this.before("*", (req) => {
console.log(
"[USER]:",
req.user.id,
" [LEVEL]: ",
req.user.attr.level,
"[ROLE]",
req.user.is("user") ? "user" : "other"
);
});
this.on("mockLogin", async (req) => {
const { email, password } = req.data;
let userFromDb = await db.run(SELECT.one(Employees).where({ email }));
let role = "employee";
if (!userFromDb) {
userFromDb = await db.run(SELECT.one(Customers).where({ email }));
role = "customer";
}
if (!userFromDb || password !== userFromDb.password) {
req.reject(401);
}
return {
mockedToken: Buffer.from(`${email}:${password}`).toString("base64"),
level: role === "customer" ? 1 : 2,
email: userFromDb.email,
ID: userFromDb.ID,
roles: [role],
};
});
};