Skip to content

Commit

Permalink
Merge pull request #3139 from gravitl/ACC-700
Browse files Browse the repository at this point in the history
ACC-700: Remove Old User Groups
  • Loading branch information
abhishek9686 authored Sep 29, 2024
2 parents c561dec + 1561aaf commit d7881c9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
3 changes: 1 addition & 2 deletions controllers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"net/http"
"net/url"
"strings"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -92,7 +91,7 @@ func userMiddleWare(handler http.Handler) http.Handler {
if userID, ok := params["username"]; ok {
r.Header.Set("TARGET_RSRC_ID", userID)
} else {
username, _ := url.QueryUnescape(r.URL.Query().Get("username"))
username := r.URL.Query().Get("username")
if username != "" {
r.Header.Set("TARGET_RSRC_ID", username)
}
Expand Down
3 changes: 1 addition & 2 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"reflect"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -240,7 +239,7 @@ func getUser(w http.ResponseWriter, r *http.Request) {
func getUserV1(w http.ResponseWriter, r *http.Request) {
// set header.
w.Header().Set("Content-Type", "application/json")
usernameFetched, _ := url.QueryUnescape(r.URL.Query().Get("username"))
usernameFetched := r.URL.Query().Get("username")
if usernameFetched == "" {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("username is required"), "badrequest"))
return
Expand Down
3 changes: 1 addition & 2 deletions logic/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logic

import (
"net/http"
"net/url"
"strings"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -97,7 +96,7 @@ func ContinueIfUserMatch(next http.Handler) http.HandlerFunc {
var params = mux.Vars(r)
var requestedUser = params["username"]
if requestedUser == "" {
requestedUser, _ = url.QueryUnescape(r.URL.Query().Get("username"))
requestedUser = r.URL.Query().Get("username")
}
if requestedUser != r.Header.Get("user") {
ReturnErrorResponse(w, r, errorResponse)
Expand Down
16 changes: 15 additions & 1 deletion migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
func Run() {
updateEnrollmentKeys()
assignSuperAdmin()
removeOldUserGrps()
syncUsers()
updateHosts()
updateNodes()
updateAcls()

}

func assignSuperAdmin() {
Expand Down Expand Up @@ -124,6 +124,20 @@ func updateEnrollmentKeys() {
}
}

func removeOldUserGrps() {
rows, err := database.FetchRecords(database.USER_GROUPS_TABLE_NAME)
if err != nil {
return
}
for key, row := range rows {
userG := models.UserGroup{}
_ = json.Unmarshal([]byte(row), &userG)
if userG.ID == "" {
database.DeleteRecord(database.USER_GROUPS_TABLE_NAME, key)
}
}
}

func updateHosts() {
rows, err := database.FetchRecords(database.HOSTS_TABLE_NAME)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions pro/controllers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func UserHandlers(r *mux.Router) {
// Responses:
// 200: ReturnSuccessResponse
func userInviteSignUp(w http.ResponseWriter, r *http.Request) {
email, _ := url.QueryUnescape(r.URL.Query().Get("email"))
code, _ := url.QueryUnescape(r.URL.Query().Get("invite_code"))
email := r.URL.Query().Get("email")
code := r.URL.Query().Get("invite_code")
in, err := logic.GetUserInvite(email)
if err != nil {
logger.Log(0, "failed to fetch users: ", err.Error())
Expand Down Expand Up @@ -133,8 +133,8 @@ func userInviteSignUp(w http.ResponseWriter, r *http.Request) {
// Responses:
// 200: ReturnSuccessResponse
func userInviteVerify(w http.ResponseWriter, r *http.Request) {
email, _ := url.QueryUnescape(r.URL.Query().Get("email"))
code, _ := url.QueryUnescape(r.URL.Query().Get("invite_code"))
email := r.URL.Query().Get("email")
code := r.URL.Query().Get("invite_code")
err := logic.ValidateAndApproveUserInvite(email, code)
if err != nil {
logger.Log(0, "failed to fetch users: ", err.Error())
Expand Down Expand Up @@ -299,7 +299,7 @@ func listUserInvites(w http.ResponseWriter, r *http.Request) {
// Responses:
// 200: ReturnSuccessResponse
func deleteUserInvite(w http.ResponseWriter, r *http.Request) {
email, _ := url.QueryUnescape(r.URL.Query().Get("invitee_email"))
email := r.URL.Query().Get("invitee_email")
err := logic.DeleteUserInvite(email)
if err != nil {
logger.Log(0, "failed to delete user invite: ", email, err.Error())
Expand Down Expand Up @@ -365,7 +365,7 @@ func listUserGroups(w http.ResponseWriter, r *http.Request) {
// 200: userBodyResponse
func getUserGroup(w http.ResponseWriter, r *http.Request) {

gid, _ := url.QueryUnescape(r.URL.Query().Get("group_id"))
gid := r.URL.Query().Get("group_id")
if gid == "" {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("group id is required"), "badrequest"))
return
Expand Down Expand Up @@ -486,14 +486,14 @@ func updateUserGroup(w http.ResponseWriter, r *http.Request) {
// @Failure 500 {object} models.ErrorResponse
func deleteUserGroup(w http.ResponseWriter, r *http.Request) {

gid, _ := url.QueryUnescape(r.URL.Query().Get("group_id"))
gid := r.URL.Query().Get("group_id")
if gid == "" {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("role is required"), "badrequest"))
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("group id is required"), "badrequest"))
return
}
userG, err := proLogic.GetUserGroup(models.UserGroupID(gid))
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("role is required"), "badrequest"))
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("failed to fetch group details"), "badrequest"))
return
}
err = proLogic.DeleteUserGroup(models.UserGroupID(gid))
Expand All @@ -512,7 +512,7 @@ func deleteUserGroup(w http.ResponseWriter, r *http.Request) {
// @Success 200 {object} []models.UserRolePermissionTemplate
// @Failure 500 {object} models.ErrorResponse
func ListRoles(w http.ResponseWriter, r *http.Request) {
platform, _ := url.QueryUnescape(r.URL.Query().Get("platform"))
platform := r.URL.Query().Get("platform")
var roles []models.UserRolePermissionTemplate
var err error
if platform == "true" {
Expand All @@ -538,7 +538,7 @@ func ListRoles(w http.ResponseWriter, r *http.Request) {
// @Success 200 {object} models.UserRolePermissionTemplate
// @Failure 500 {object} models.ErrorResponse
func getRole(w http.ResponseWriter, r *http.Request) {
rid, _ := url.QueryUnescape(r.URL.Query().Get("role_id"))
rid := r.URL.Query().Get("role_id")
if rid == "" {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("role is required"), "badrequest"))
return
Expand Down Expand Up @@ -628,7 +628,7 @@ func updateRole(w http.ResponseWriter, r *http.Request) {
// @Failure 500 {object} models.ErrorResponse
func deleteRole(w http.ResponseWriter, r *http.Request) {

rid, _ := url.QueryUnescape(r.URL.Query().Get("role_id"))
rid := r.URL.Query().Get("role_id")
if rid == "" {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("role is required"), "badrequest"))
return
Expand Down
7 changes: 5 additions & 2 deletions pro/logic/user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func UserRolesInit() {
}

func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
if netID.String() == "" {
return
}
var NetworkAdminPermissionTemplate = models.UserRolePermissionTemplate{
ID: models.UserRoleID(fmt.Sprintf("%s-%s", netID, models.NetworkAdmin)),
Default: true,
Expand Down Expand Up @@ -120,7 +123,7 @@ func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
models.UserRoleID(fmt.Sprintf("%s-%s", netID, models.NetworkAdmin)): {},
},
},
MetaData: "The network role was automatically created by Netmaker.",
MetaData: "The network group was automatically created by Netmaker.",
}
var NetworkUserGroup = models.UserGroup{
ID: models.UserGroupID(fmt.Sprintf("%s-%s-grp", netID, models.NetworkUser)),
Expand All @@ -129,7 +132,7 @@ func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
models.UserRoleID(fmt.Sprintf("%s-%s", netID, models.NetworkUser)): {},
},
},
MetaData: "The network role was automatically created by Netmaker.",
MetaData: "The network group was automatically created by Netmaker.",
}
d, _ = json.Marshal(NetworkAdminGroup)
database.Insert(NetworkAdminGroup.ID.String(), string(d), database.USER_GROUPS_TABLE_NAME)
Expand Down

0 comments on commit d7881c9

Please sign in to comment.