Browse Source
lucifer-server, controllers, sqlite, models: Added read only API for groups and stuff.
login_bugfix
lucifer-server, controllers, sqlite, models: Added read only API for groups and stuff.
login_bugfix
Gisle Aune
5 years ago
5 changed files with 230 additions and 8 deletions
-
2cmd/lucifer-server/main.go
-
152controllers/group-controller.go
-
2controllers/user-controller.go
-
63database/sqlite/group-repository.go
-
19models/group.go
@ -0,0 +1,152 @@ |
|||
package controllers |
|||
|
|||
import ( |
|||
"database/sql" |
|||
"fmt" |
|||
"log" |
|||
"net/http" |
|||
"strconv" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/internal/respond" |
|||
"git.aiterp.net/lucifer/lucifer/models" |
|||
"github.com/gorilla/mux" |
|||
) |
|||
|
|||
// The GroupController is a controller for all user inports.
|
|||
type GroupController struct { |
|||
groups models.GroupRepository |
|||
users models.UserRepository |
|||
lights models.LightRepository |
|||
} |
|||
|
|||
// getGroups (`GET /:id`): Get user by id
|
|||
func (c *GroupController) getGroups(w http.ResponseWriter, r *http.Request) { |
|||
session := models.SessionFromContext(r.Context()) |
|||
if session == nil { |
|||
respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in") |
|||
return |
|||
} |
|||
|
|||
user, err := c.users.FindByID(r.Context(), session.UserID) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in") |
|||
return |
|||
} |
|||
|
|||
groups, err := c.groups.ListByUser(r.Context(), user) |
|||
if err != nil && err != sql.ErrNoRows { |
|||
log.Printf("Getting groups for user %s (%d) failed: %s", user.Name, user.ID, err) |
|||
respond.Error(w, http.StatusInternalServerError, "internal_error", "Failed to get groups.") |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, groups) |
|||
} |
|||
|
|||
// getGroup (`GET /:id`): Get user by id
|
|||
func (c *GroupController) getGroup(w http.ResponseWriter, r *http.Request) { |
|||
session := models.SessionFromContext(r.Context()) |
|||
if session == nil { |
|||
respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in") |
|||
return |
|||
} |
|||
|
|||
idStr := mux.Vars(r)["id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id"+idStr+"is not valid.") |
|||
return |
|||
} |
|||
|
|||
group, err := c.groups.FindByID(r.Context(), int(id)) |
|||
if err != nil || !group.Permission(session.UserID).Read { |
|||
respond.Error(w, http.StatusNotFound, "not_found", "The group cannot be found or you are not authorized to view it.") |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, group) |
|||
} |
|||
|
|||
// getGroupLights (`GET /:id/light/`): Get user by id
|
|||
func (c *GroupController) getGroupLights(w http.ResponseWriter, r *http.Request) { |
|||
session := models.SessionFromContext(r.Context()) |
|||
if session == nil { |
|||
respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in") |
|||
return |
|||
} |
|||
|
|||
idStr := mux.Vars(r)["id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The id "+idStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
group, err := c.groups.FindByID(r.Context(), int(id)) |
|||
if err != nil || !group.Permission(session.UserID).Read { |
|||
respond.Error(w, http.StatusNotFound, "group_not_found", "The group cannot be found or you are not authorized to view it.") |
|||
return |
|||
} |
|||
|
|||
lights, err := c.lights.ListByGroup(r.Context(), group) |
|||
if err != nil && err != sql.ErrNoRows { |
|||
log.Printf("Getting lights for group %s (%d) failed: %s", group.Name, group.ID, err) |
|||
respond.Error(w, http.StatusInternalServerError, "internal_error", "Failed to get groups.") |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, lights) |
|||
} |
|||
|
|||
// getGroupLight (`GET /:group_id/light/:id`): Get user by id
|
|||
func (c *GroupController) getGroupLight(w http.ResponseWriter, r *http.Request) { |
|||
session := models.SessionFromContext(r.Context()) |
|||
if session == nil { |
|||
respond.Error(w, http.StatusForbidden, "permission_denied", "You must log in") |
|||
return |
|||
} |
|||
|
|||
groupIDStr := mux.Vars(r)["group_id"] |
|||
groupID, err := strconv.ParseInt(groupIDStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The group id "+groupIDStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
idStr := mux.Vars(r)["id"] |
|||
id, err := strconv.ParseInt(idStr, 10, 32) |
|||
if err != nil { |
|||
respond.Error(w, http.StatusBadRequest, "invalid_id", "The light id "+idStr+" is not valid.") |
|||
return |
|||
} |
|||
|
|||
group, err := c.groups.FindByID(r.Context(), int(groupID)) |
|||
if err != nil || !group.Permission(session.UserID).Read { |
|||
respond.Error(w, http.StatusNotFound, "group_not_found", "The group cannot be found or you are not authorized to view it.") |
|||
return |
|||
} |
|||
|
|||
light, err := c.lights.FindByID(r.Context(), int(id)) |
|||
if err != nil || light.GroupID != group.ID { |
|||
fmt.Println(light, id) |
|||
respond.Error(w, http.StatusNotFound, "light_not_found", "The light cannot be found in this group.") |
|||
return |
|||
} |
|||
|
|||
respond.Data(w, light) |
|||
} |
|||
|
|||
// Mount mounts the controller
|
|||
func (c *GroupController) Mount(router *mux.Router, prefix string) { |
|||
sub := router.PathPrefix(prefix).Subrouter() |
|||
|
|||
sub.HandleFunc("/", c.getGroups).Methods("GET") |
|||
sub.HandleFunc("/{id}", c.getGroup).Methods("GET") |
|||
sub.HandleFunc("/{id}/light/", c.getGroupLights).Methods("GET") |
|||
sub.HandleFunc("/{group_id}/light/{id}", c.getGroupLight).Methods("GET") |
|||
} |
|||
|
|||
// NewGroupController creates a new GroupController.
|
|||
func NewGroupController(groups models.GroupRepository, users models.UserRepository, lights models.LightRepository) *GroupController { |
|||
return &GroupController{groups: groups, users: users, lights: lights} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue