Example #1
0
func Example() {
	p := permission.Permission{
		Name: "user",
		Sub:  "edit",
	}

	data, _ := json.Marshal(p)
	fmt.Printf("%s\n", data)

	p = permission.Permission{}
	json.Unmarshal(data, &p)
	fmt.Println(p.Name)
	fmt.Println(p.Sub)

	def := permission.Definition{
		Name:          "user",
		Subset:        []string{"edit", "profile", "email", "friends", "about"},
		DefaultSubset: []string{"profile", "about"},
	}

	required, _ := permission.Parse("user.edit")
	allowed := def.Allowed(required, p)
	fmt.Println(allowed)
	// Output:
	// "user.edit"
	// user
	// edit
	// true
}
Example #2
0
func ExampleDefinition() {
	def := permission.Definition{
		Name:          "file",
		Subset:        []string{"read", "write", "execute"},
		DefaultSubset: []string{"read", "execute"},
	}

	required, _ := permission.Parse("file.read")

	p, _ := permission.Parse("file.read")
	fmt.Println(def.Allowed(required, p))

	p, _ = permission.Parse("file.execute")
	fmt.Println(def.Allowed(required, p))

	p, _ = permission.Parse("file") // file = file.read,file.execute
	fmt.Println(def.Allowed(required, p))

	required, _ = permission.Parse("file.write")

	p, _ = permission.Parse("file") // file = file.read,file.execute
	fmt.Println(def.Allowed(required, p))
	// Output:
	// true
	// false
	// true
	// false
}