Skip to content

Commit

Permalink
Make permissions match flags when removing them
Browse files Browse the repository at this point in the history
  • Loading branch information
HopeBaron committed Jul 6, 2020
1 parent 8478199 commit 2307c5c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.5.6

## Fixes
* Fixed incorrect calculations of denied `Permissions`.

# 0.5.5

## Additions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ import kotlinx.serialization.*
@Serializable(with = Permissions.Companion::class)
class Permissions constructor(val code: Int) {

operator fun plus(permission: Permission): Permissions = when {
code and permission.code == permission.code -> this
else -> Permissions(this.code or permission.code)
}
operator fun plus(permission: Permission): Permissions = Permissions(this.code or permission.code)


operator fun minus(permission: Permission): Permissions = Permissions(code xor (code and permission.code))

operator fun minus(permission: Permission): Permissions = when {
code and permission.code == permission.code -> Permissions(code xor permission.code)
else -> this
}

operator fun contains(permission: Permission): Boolean {
return this.code and permission.code == permission.code
Expand Down Expand Up @@ -52,19 +48,15 @@ class Permissions constructor(val code: Int) {
}

operator fun Permissions.unaryMinus() {
if (this@PermissionsBuilder.code and code == code) {
this@PermissionsBuilder.code = this@PermissionsBuilder.code xor code
}
this@PermissionsBuilder.code = this@PermissionsBuilder.code xor (this@PermissionsBuilder.code and code)
}

operator fun Permission.unaryPlus() {
this@PermissionsBuilder.code = this@PermissionsBuilder.code or code
}

operator fun Permission.unaryMinus() {
if (this@PermissionsBuilder.code and code == code) {
this@PermissionsBuilder.code = this@PermissionsBuilder.code xor code
}
this@PermissionsBuilder.code = this@PermissionsBuilder.code xor (this@PermissionsBuilder.code and code)
}

fun permissions() = Permissions(code)
Expand Down

0 comments on commit 2307c5c

Please sign in to comment.