Skip to content

Commit

Permalink
Closes #54 - Added a get user api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lfkeitel committed Mar 18, 2018
1 parent f598a41 commit 1426f6b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
27 changes: 24 additions & 3 deletions src/controllers/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ func NewUserController(e *common.Environment) *UserController {
return &UserController{e: e}
}

func (u *UserController) UserHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
if r.Method == "POST" {
func (u *UserController) UserHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
switch r.Method {
case "POST":
u.saveUserHandler(w, r)
} else if r.Method == "DELETE" {
case "DELETE":
u.deleteUserHandler(w, r)
case "GET":
u.getUserHandler(w, r, p)
}
}

Expand Down Expand Up @@ -308,3 +311,21 @@ func (u *UserController) deleteUserHandler(w http.ResponseWriter, r *http.Reques
}).Info("User deleted")
common.NewAPIResponse("User deleted", nil).WriteResponse(w, http.StatusNoContent)
}

func (u *UserController) getUserHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
sessionUser := models.GetUserFromContext(r)
usernameParam := p.ByName("username")

user, err := stores.GetUserStore(u.e).GetUserByUsername(usernameParam)
if err != nil {
http.Error(w, "Error getting user from database", http.StatusInternalServerError)
return
}

if user.Username != sessionUser.Username && !sessionUser.Can(models.ViewUsers) {
common.NewAPIResponse("Unauthorized", nil).WriteResponse(w, http.StatusUnauthorized)
return
}

common.NewAPIResponse("", user).WriteResponse(w, http.StatusOK)
}
29 changes: 29 additions & 0 deletions src/models/deviceExpiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package models

import (
"encoding/json"
"time"

"github.com/packet-guardian/packet-guardian/src/common"
Expand All @@ -25,13 +26,41 @@ const (
UserDeviceLimitUnlimited UserDeviceLimit = 0
)

func (ue UserExpiration) string() string {
switch ue {
case UserDeviceExpirationNever:
return "never"
case UserDeviceExpirationGlobal:
return "global"
case UserDeviceExpirationSpecific:
return "specific-datetime"
case UserDeviceExpirationDuration:
return "duration"
case UserDeviceExpirationDaily:
return "daily-time"
case UserDeviceExpirationRolling:
return "rolling-duration"
}
return ""
}

var globalDeviceExpiration *UserDeviceExpiration

type UserDeviceExpiration struct {
Mode UserExpiration
Value int64 // Daily and Duration, time in seconds. Specific, unix epoch
}

func (ude *UserDeviceExpiration) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Mode string `json:"mode"`
Value int64 `json:"value"`
}{
Mode: ude.Mode.string(),
Value: ude.Value,
})
}

func GetGlobalDefaultExpiration(e *common.Environment) *UserDeviceExpiration {
g := &UserDeviceExpiration{} // Defaults to never
switch e.Config.Registration.DefaultDeviceExpirationType {
Expand Down
48 changes: 32 additions & 16 deletions src/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package models

import (
"encoding/json"
"time"

"golang.org/x/crypto/bcrypt"
Expand All @@ -22,25 +23,25 @@ type UserStore interface {
type User struct {
e *common.Environment
store UserStore
ID int
Username string
Password string
HasPassword bool
ID int `json:"-"`
Username string `json:"username"`
Password string `json:"-"`
HasPassword bool `json:"has_password"`
savePassword bool
ClearPassword bool
DeviceLimit UserDeviceLimit
DeviceExpiration *UserDeviceExpiration
ValidStart time.Time
ValidEnd time.Time
ValidForever bool
CanManage bool
CanAutoreg bool
ClearPassword bool `json:"-"`
DeviceLimit UserDeviceLimit `json:"device_limit"`
DeviceExpiration *UserDeviceExpiration `json:"device_expiration"`
ValidStart time.Time `json:"-"`
ValidEnd time.Time `json:"-"`
ValidForever bool `json:"valid_forever"`
CanManage bool `json:"can_manage"`
CanAutoreg bool `json:"can_autoreg"`
blacklist BlacklistItem
Rights Permission
Rights Permission `json:"-"`

UIGroup string
APIGroup string
AllowStatusAPI bool
UIGroup string `json:"-"`
APIGroup string `json:"-"`
AllowStatusAPI bool `json:"-"`
}

// NewUser creates a new base user
Expand Down Expand Up @@ -83,6 +84,21 @@ func (u *User) LoadRights() {
}
}

func (u *User) MarshalJSON() ([]byte, error) {
type Alias User
return json.Marshal(&struct {
*Alias
ValidStart time.Time `json:"valid_start"`
ValidEnd time.Time `json:"valid_end"`
Blacklisted bool `json:"blacklisted"`
}{
Alias: (*Alias)(u),
ValidStart: u.ValidStart.UTC(),
ValidEnd: u.ValidEnd.UTC(),
Blacklisted: u.IsBlacklisted(),
})
}

func (u *User) IsNew() bool {
return (u.ID == 0 || u.Username == "")
}
Expand Down
1 change: 1 addition & 0 deletions src/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func apiRouter(e *common.Environment) http.Handler {
userAPIController := api.NewUserController(e)
r.POST("/api/user", userAPIController.UserHandler)
r.DELETE("/api/user", userAPIController.UserHandler)
r.GET("/api/user/:username", userAPIController.UserHandler)

statusAPIController := api.NewStatusController(e)
r.GET("/api/status", statusAPIController.GetStatus)
Expand Down

0 comments on commit 1426f6b

Please sign in to comment.