Skip to content

Commit

Permalink
Add public methods to check if permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
brutella committed Oct 12, 2020
1 parent 4578fc3 commit a19a6b1
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions characteristic/characteristic.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,18 @@ func (c *Characteristic) Equal(other interface{}) bool {

// Private

func (c *Characteristic) isReadable() bool {
func (c *Characteristic) IsReadable() bool {
return readPerm(c.Perms)
}

func (c *Characteristic) isWritable() bool {
func (c *Characteristic) IsWritable() bool {
return writePerm(c.Perms)
}

func (c *Characteristic) IsObservable() bool {
return eventPerm(c.Perms)
}

func (c *Characteristic) getValue(conn net.Conn) interface{} {
if c.valueGetFunc != nil {
c.updateValue(c.valueGetFunc(), conn, false)
Expand Down Expand Up @@ -130,12 +134,12 @@ func (c *Characteristic) updateValue(value interface{}, conn net.Conn, checkPerm
}

// Ignore new values from remote when permissions don't allow write and checkPerms is true
if checkPerms && !c.isWritable() {
if checkPerms && !c.IsWritable() {
return
}

old := c.Value
if c.isReadable() {
if c.IsReadable() {
c.Value = value
}

Expand Down Expand Up @@ -223,3 +227,13 @@ func writePerm(permissions []string) bool {
}
return false
}

// eventPerm returns true when perms include events permission
func eventPerm(permissions []string) bool {
for _, value := range permissions {
if value == PermEvents {
return true
}
}
return false
}

0 comments on commit a19a6b1

Please sign in to comment.