Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
perf: optimize access control method on user (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Jul 11, 2024
1 parent f0e58b6 commit bb6b7e0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
_ "embed"
"os"
"slices"

tte "github.com/zurvan-lab/TimeTrace/utils/errors"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -127,3 +128,11 @@ func (conf *Config) ToYAML() ([]byte, error) {

return buf.Bytes(), nil
}

func (u *User) HasAccess(c string) bool {
if u.Cmds[0] == "*" {
return true
}

return slices.Contains(u.Cmds, c)
}
19 changes: 1 addition & 18 deletions core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ func (s *Server) HandleConnection(conn net.Conn) {

query := parser.ParseQuery(string(buffer[:n]))

access := s.HaveAccess(*user, query.Command)
if access {
if user.HasAccess(query.Command) {
result := execute.Execute(query, s.db)

_, err = conn.Write([]byte(result))
Expand Down Expand Up @@ -175,22 +174,6 @@ func (s *Server) Authenticate(conn net.Conn) (*config.User, error) {
return user, nil
}

func (s *Server) HaveAccess(user config.User, command string) bool {
access := false

for _, c := range user.Cmds {
if c == command {
access = true
}
}

if user.Cmds[0] == "*" {
access = true
}

return access
}

func (s *Server) Stop() {
s.ActiveConnsMux.Lock()
defer s.ActiveConnsMux.Unlock()
Expand Down

0 comments on commit bb6b7e0

Please sign in to comment.