Skip to content

Commit 546b355

Browse files
authored
Merge branch 'main' into knewbury01/fn-cap-sources
2 parents da65203 + 5f2d430 commit 546b355

60 files changed

Lines changed: 1418 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* 1. Creating a custom middleware that fulfills `req.user` */
2+
3+
const cds = require("@sap/cds");
4+
class CustomPrivilegedUser1 extends cds.User {
5+
is() {
6+
return true;
7+
}
8+
}
9+
module.exports = (req, res, next) => {
10+
req.user = new CustomPrivilegedUser1("privileged");
11+
next();
12+
};
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const cds = require("@sap/cds");
2+
const makePrivilegedUser = require("./custom-auth.js");
3+
4+
class CustomPrivilegedUser1 extends cds.User {
5+
is() {
6+
return true;
7+
}
8+
}
9+
10+
class CustomPrivilegedUser2 extends cds.User {
11+
is() {
12+
return 1 == 1;
13+
}
14+
}
15+
16+
class CustomPrivilegedUser3 extends cds.User {
17+
is() {
18+
const variable = 1;
19+
return variable == variable;
20+
}
21+
}
22+
23+
class CustomPrivilegedUser4 extends cds.User {
24+
is() {
25+
const condition = true;
26+
if (condition) {
27+
return true;
28+
} else {
29+
return true;
30+
}
31+
}
32+
}
33+
34+
class CustomPrivilegedUser5 extends cds.User {
35+
is() {
36+
switch (condition) {
37+
case "hihi":
38+
return true;
39+
case "hoho":
40+
return true;
41+
}
42+
}
43+
}
44+
45+
class Service1 extends cds.ApplicationService {
46+
init() {
47+
/* 1. Using the function exported by the middleware directly */
48+
this.on("send1", async (req) => {
49+
const user = makePrivilegedUser(req, {}, () => { });
50+
return this.tx({ user }, (tx) =>
51+
tx.run(
52+
INSERT.into("Service1Entity").entries({
53+
url: req._.req.url,
54+
user: req.user.id,
55+
data: req.data.messageToPass,
56+
}),
57+
),
58+
);
59+
});
60+
/* 2. Creating a cds.User.Privileged directly */
61+
this.on("send2", async (msg) => {
62+
const user1 = new cds.User.Privileged("privileged1");
63+
this.tx({ user1 }, (tx) =>
64+
tx.run(
65+
INSERT.into("Service2Entity").entries({
66+
url: req._.req.url,
67+
user: req.user.id,
68+
data: msg.data.messageToPass,
69+
}),
70+
),
71+
);
72+
});
73+
/* 3. Creating a custom privileged user directly */
74+
this.on("send3", async (msg) => {
75+
const user2 = new CustomPrivilegedUser1("privileged2");
76+
this.tx({ user2 }, (tx) =>
77+
tx.run(
78+
INSERT.into("Service2Entity").entries({
79+
url: req._.req.url,
80+
user: req.user.id,
81+
}),
82+
),
83+
);
84+
});
85+
}
86+
}

0 commit comments

Comments
 (0)