-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 20905ee
Showing
7 changed files
with
941 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
[](https://github.com/barbarbar338/perman) | ||
[](https://github.com/barbarbar338/perman) | ||
[](https://discord.gg/BjEJFwh) | ||
[](https://github.com/barbarbar338/perman) | ||
[](https://github.com/barbarbar338/perman) | ||
|
||
<p align="center"> | ||
<img src="https://raw.githubusercontent.com/barbarbar338/readme-template/main/icon.png" alt="Logo" width="160" height="160" /> | ||
<h3 align="center">Perman.GO</h3> | ||
|
||
<p align="center"> | ||
🔑 Permission management made easy | ||
<br /> | ||
<a href="https://discord.gg/BjEJFwh"><strong>Get support »</strong></a> | ||
<br /> | ||
<br /> | ||
<a href="https://github.com/barbarbar338/perman/issues">Report Bug</a> | ||
· | ||
<a href="https://github.com/barbarbar338/perman/issues">Request Feature</a> | ||
· | ||
<a href="https://338.rocks">Webpage</a> | ||
</p> | ||
</p> | ||
|
||
# 🔑 Perman | ||
|
||
Permission management made easy | ||
|
||
# 📦 Installation | ||
|
||
- Run `go get github.com/barbarbar338/perman` | ||
|
||
# 🤓 Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"github.com/barbarbar338/perman" | ||
) | ||
|
||
func main() { | ||
|
||
} | ||
``` | ||
|
||
# 📄 License | ||
|
||
Copyright © 2022 [Barış DEMİRCİ](https://github.com/barbarbar338). | ||
|
||
Distributed under the [GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html) License. See `LICENSE` for more information. | ||
|
||
# 🧦 Contributing | ||
|
||
Fell free to use GitHub's features. | ||
|
||
1. Fork the Project | ||
2. Create your Feature Branch (`git checkout -b feature/my-feature`) | ||
3. Commit your Changes (`git commit -m 'my awesome feature my-feature'`) | ||
4. Push to the Branch (`git push origin feature/my-feature`) | ||
5. Open a Pull Request | ||
|
||
# 🔥 Show your support | ||
|
||
Give a ⭐️ if this project helped you! | ||
|
||
# 📞 Contact | ||
|
||
- Mail: [email protected] | ||
- Discord: https://discord.gg/BjEJFwh | ||
- Instagram: https://www.instagram.com/ben_baris.d/ | ||
- Webpage: https://338.rocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/barbarbar338/perman | ||
|
||
go 1.18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package perman | ||
|
||
func (p Perman) Serialize(flags []string) int64 { | ||
if len(flags) < 1 { | ||
return 0 | ||
} | ||
|
||
res := int64(0) | ||
for _, flag := range flags { | ||
res |= p.Get(flag) | ||
} | ||
|
||
return res | ||
} | ||
|
||
func (p Perman) Deserialize(permission int64) []string { | ||
if permission == 0 { | ||
return []string{} | ||
} | ||
|
||
res := make([]string, 0) | ||
for key, value := range p.flags { | ||
if permission&value != 0 { | ||
res = append(res, key) | ||
} | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package perman | ||
|
||
import "math" | ||
|
||
type Perman struct { | ||
flags map[string]int64 | ||
} | ||
|
||
func Factory(flags []string) Perman { | ||
p := Perman{ | ||
flags: make(map[string]int64), | ||
} | ||
for i, flag := range flags { | ||
p.flags[flag] = int64(math.Pow(2, float64(i))) | ||
} | ||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package perman | ||
|
||
func (p Perman) Match(permission int64, flags []string) bool { | ||
if len(flags) < 1 { | ||
return true | ||
} | ||
|
||
for _, flag := range flags { | ||
if permission&p.Get(flag) != p.Get(flag) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (p Perman) MatchAll(permission int64, flags []string) bool { | ||
if len(flags) < 1 { | ||
return true | ||
} | ||
|
||
for _, flag := range flags { | ||
if permission&p.Get(flag) != p.Get(flag) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (p Perman) HasAll(permission int64, flags []string) bool { | ||
if len(flags) < 1 { | ||
return true | ||
} | ||
|
||
for _, flag := range flags { | ||
if permission&p.Get(flag) != p.Get(flag) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (p Perman) Some(permission int64, flags []string) bool { | ||
if len(flags) < 1 { | ||
return true | ||
} | ||
|
||
for _, flag := range flags { | ||
if permission&p.Get(flag) == p.Get(flag) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (p Perman) HasSome(permission int64, flags []string) bool { | ||
if len(flags) < 1 { | ||
return true | ||
} | ||
|
||
for _, flag := range flags { | ||
if permission&p.Get(flag) == p.Get(flag) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (p Perman) HasNone(permission int64, flags []string) bool { | ||
if len(flags) < 1 { | ||
return true | ||
} | ||
|
||
for _, flag := range flags { | ||
if permission&p.Get(flag) == p.Get(flag) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (p Perman) None(permission int64, flags []string) bool { | ||
if len(flags) < 1 { | ||
return true | ||
} | ||
|
||
for _, flag := range flags { | ||
if permission&p.Get(flag) == p.Get(flag) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func (p Perman) Has(permission int64, flag string) bool { | ||
return permission&p.Get(flag) == p.Get(flag) | ||
} | ||
|
||
func (p Perman) Test(permission int64, flag string) bool { | ||
return permission&p.Get(flag) == p.Get(flag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package perman | ||
|
||
func (p Perman) Keys() []string { | ||
keys := make([]string, 0) | ||
for key := range p.flags { | ||
keys = append(keys, key) | ||
} | ||
return keys | ||
} | ||
|
||
func (p Perman) Values() []int64 { | ||
values := make([]int64, 0) | ||
for flag := range p.flags { | ||
values = append(values, p.flags[flag]) | ||
} | ||
return values | ||
} | ||
|
||
func (p Perman) Get(flag string) int64 { | ||
value, ok := p.flags[flag] | ||
if ok { | ||
return value | ||
} else { | ||
return int64(0) | ||
} | ||
} | ||
|
||
func (p Perman) Add(permission int64, key string) int64 { | ||
oldValues := p.Deserialize(permission) | ||
newValues := append(oldValues, key) | ||
return p.Serialize(newValues) | ||
} | ||
|
||
func (p Perman) Remove(permission int64, key string) int64 { | ||
oldValues := p.Deserialize(permission) | ||
newValues := make([]string, 0) | ||
for _, value := range oldValues { | ||
if value != key { | ||
newValues = append(newValues, value) | ||
} | ||
} | ||
return p.Serialize(newValues) | ||
} | ||
|
||
func (p Perman) Full() int64 { | ||
return p.Serialize(p.Keys()) | ||
} |