-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathladmon_test.go
More file actions
59 lines (51 loc) · 1.02 KB
/
ladmon_test.go
File metadata and controls
59 lines (51 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ladmon
import (
"context"
"fmt"
"github.com/ory/ladon"
"testing"
"time"
)
func Example() {
// Create manager
manager, err := NewMongoManager(
"mongodb://localhost:27017",
"ladon",
20*time.Second,
)
if err != nil {
panic(err)
}
defer manager.Close()
// Use with Ladon
warden := &ladon.Ladon{
Manager: manager,
}
// Create policy
policy := &ladon.DefaultPolicy{
ID: "3",
Description: "Test policy 3",
Subjects: []string{"user:3", "group:admin"},
Resources: []string{"resource:3"},
Actions: []string{"create", "read"},
Effect: ladon.AllowAccess,
}
// Save policy
if err := manager.Create(context.Background(), policy); err != nil {
panic(err)
}
// Check permission
request := &ladon.Request{
Subject: "user:3",
Resource: "resource:3",
Action: "read",
}
if err := warden.IsAllowed(context.TODO(), request); err != nil {
fmt.Println("Access denied:", err)
} else {
fmt.Println("Access allowed")
}
}
func Test_Example(t *testing.T) {
Example()
}