Skip to content

Commit

Permalink
added addUserToGroupById
Browse files Browse the repository at this point in the history
added removeUserFromGroupById
  • Loading branch information
menkveldj committed Jan 10, 2018
1 parent dcf2973 commit c41a6c6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion context/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package consts
const USER_KEY_FOR_GIN_CONTEXT = "user"
const GOCMS_HEADER_USER_CONTEXT_KEY = "X-GOCMS-USER-CONTEXT"
const GOCMS_HEADER_TIMEZONE_KEY = "X-GOCMS-TIMEZONE"
const GOCMS_HEADER_MICROSERVICE_SECRET = "X-MS-SECRET"
const GOCMS_HEADER_MICROSERVICE_SECRET = "X-GOCMS-MICROSERVICE-SECRET"

const GOCMS_MIDDLEWARE_URL_SEGMENT = "middleware"
57 changes: 57 additions & 0 deletions domain/acl/group/group_controller/internal_group_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"github.com/gocms-io/gocms/init/service"
"github.com/gocms-io/gocms/routes"
"net/http"
"strconv"
"github.com/gocms-io/gocms/utility/errors"
"github.com/gocms-io/gocms/utility/sqlUtl"
)

type InternalGroupController struct {
Expand All @@ -23,6 +26,7 @@ func DefaultInternalGroupController(iRoutes *routes.InternalRoutes, sg *service.

func (ec *InternalGroupController) InternalDefault() {
ec.internalRoutes.InternalRoot.POST("/acl/addUser/:userId/toGroupByName/:groupName", ec.addUserToGroupByName)
ec.internalRoutes.InternalRoot.DELETE("/acl/removeUser/:userId/fromGroupByName/:groupName", ec.removeUserFromGroupByName)
}

/**
Expand All @@ -33,5 +37,58 @@ func (ec *InternalGroupController) InternalDefault() {
*/
func (ec *InternalGroupController) addUserToGroupByName(c *gin.Context) {

userIdStr := c.Param("userId")
userId, err := strconv.ParseInt(userIdStr,10,64)
if err != nil {
errors.Response(c, http.StatusBadRequest, "userId is missing or not an integer", err)
return
}

groupName := c.Param("groupName")
if groupName == "" {
errors.Response(c, http.StatusBadRequest, "groupName is missing", err)
return
}

err = ec.servicesGroup.GroupService.AddUserToGroupByName(userId, groupName)
if err != nil {
if sqlUtl.ErrDupEtry(err) {
errors.Response(c, http.StatusBadRequest, "User is already a member of this group", err)
return
}
errors.Response(c, http.StatusInternalServerError, "There was an error adding the user to the group specified", err)
return
}

c.Status(http.StatusOK)
}

/**
* @api {delete} (internal)/acl/removeUser/:userId/fromGroupByName/:groupName (Internal) Remove User From Group By Name
* @apiName RemoveUserFromGroup
* @apiGroup (Internal) ACL
* @apiDescription (Internal) remove a user from an acl group by userId and groupName
*/
func (ec *InternalGroupController) removeUserFromGroupByName(c *gin.Context) {

userIdStr := c.Param("userId")
userId, err := strconv.ParseInt(userIdStr,10,64)
if err != nil {
errors.Response(c, http.StatusBadRequest, "userId is missing or not an integer", err)
return
}

groupName := c.Param("groupName")
if groupName == "" {
errors.Response(c, http.StatusBadRequest, "groupName is missing", err)
return
}

err = ec.servicesGroup.GroupService.RemoveUserFromGroupByName(userId, groupName)
if err != nil {
errors.Response(c, http.StatusInternalServerError, "There was an error remove the user to the group specified", err)
return
}

c.Status(http.StatusOK)
}
1 change: 1 addition & 0 deletions init/database/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rubenv/sql-migrate"
)


type SQL struct {
Dbx *sqlx.DB
migrations *migrate.MemoryMigrationSource
Expand Down
21 changes: 17 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ type InternalEngine struct {
Database *database.Database
}

type gocmsFlags struct {

}

type gocmsRuntimeSettings struct {
help bool
port string
msPort string
noExtneralServices bool
Expand Down Expand Up @@ -105,16 +110,20 @@ func Default() (e *Engine, ie *InternalEngine) {

func (engine *Engine) Listen(uri string) error {

log.Infof("Listening on: %v\n", uri)
err := http.ListenAndServe(uri, engine.Gin)
if err == nil {
log.Infof("Listening on: %v\n", uri)
}
return err

}

func (engine *InternalEngine) Listen(uri string) error {

log.Infof("(Internal API) Listening on: %v\n", uri)
err := http.ListenAndServe(uri, engine.Gin)
if err == nil {
log.Infof("(Internal API) Listening on: %v\n", uri)
}
return err

}
Expand Down Expand Up @@ -146,16 +155,20 @@ func main() {
}
}


func getRuntimeSettings() *gocmsRuntimeSettings {


// define flags
portFlag := flag.String("port", "", "port to run on. Overrides all.")
msPortFlag := flag.String("msPort", "", "msPort to run on. Overrides all.")
noExternalServiceFlag := flag.Bool("noExternal", false, "noExternal when this flag is set gocms will not run external services.")
runIternalServiceFlag := flag.Bool("runInternal", false, "runInternal when this flag is set gocms will run internal services.")
runInternalServiceFlag := flag.Bool("runInternal", false, "runInternal when this flag is set gocms will run internal services.")
flag.Parse()


noExternalService := *noExternalServiceFlag
runInternalService := *runIternalServiceFlag
runInternalService := *runInternalServiceFlag

///////// PORT ///////////
// get server port in order of importance
Expand Down
11 changes: 11 additions & 0 deletions utility/sqlUtl/sqlUtl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sqlUtl

import (
"strings"
)

const errDupEtryMsg = "Error 1062: Duplicate entry"

func ErrDupEtry(e error) bool{
return strings.Contains(e.Error(), errDupEtryMsg)
}

0 comments on commit c41a6c6

Please sign in to comment.