2018-11-06 11:31:49 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-11-15 18:23:53 +00:00
|
|
|
"fmt"
|
2018-11-15 17:11:09 +00:00
|
|
|
"multitenantStack/constants"
|
2018-11-06 11:31:49 +00:00
|
|
|
"multitenantStack/models"
|
2018-11-15 17:11:09 +00:00
|
|
|
"multitenantStack/services/companydb"
|
2018-11-15 18:23:53 +00:00
|
|
|
tokenTools "multitenantStack/services/tokenTools"
|
2018-11-06 11:31:49 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-11-15 17:11:09 +00:00
|
|
|
"github.com/astaxie/beego/orm"
|
2018-11-06 11:31:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CompanyUserController operations for CompanyUser
|
|
|
|
type CompanyUserController struct {
|
2018-11-15 17:11:09 +00:00
|
|
|
BaseAPIController
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// URLMapping ...
|
|
|
|
func (c *CompanyUserController) URLMapping() {
|
|
|
|
c.Mapping("Post", c.Post)
|
|
|
|
c.Mapping("GetOne", c.GetOne)
|
|
|
|
c.Mapping("GetAll", c.GetAll)
|
|
|
|
c.Mapping("Put", c.Put)
|
|
|
|
c.Mapping("Delete", c.Delete)
|
2018-11-16 10:48:45 +00:00
|
|
|
c.Mapping("DeleteCompany", c.DeleteCompany)
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post ...
|
|
|
|
// @Title Post
|
2018-11-15 18:23:53 +00:00
|
|
|
// @Description Create a new CompanyUser and his user company mapping
|
2018-11-06 11:31:49 +00:00
|
|
|
// @Param body body models.CompanyUser true "body for CompanyUser content"
|
|
|
|
// @Success 201 {int} models.CompanyUser
|
|
|
|
// @Failure 403 body is empty
|
|
|
|
// @router / [post]
|
|
|
|
func (c *CompanyUserController) Post() {
|
2018-11-15 18:23:53 +00:00
|
|
|
email := c.GetString("email")
|
|
|
|
password := c.GetString("password")
|
|
|
|
name := c.GetString("name")
|
|
|
|
|
|
|
|
if email == "" || password == "" || name == "" {
|
|
|
|
c.ServeJSONError("Email/Password/Name missing")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
systemdb := companydb.GetSystemDatabase()
|
|
|
|
|
|
|
|
if systemdb == nil {
|
|
|
|
c.ServeJSONError("Error retrieving User")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
systemO, err := orm.NewOrmWithDB("postgres", "default", systemdb)
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Error retrieving User")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ucmExists, err := models.GetUserCompanyMapByEmail(systemO, email)
|
|
|
|
if ucmExists != nil {
|
|
|
|
fmt.Println(ucmExists)
|
|
|
|
c.ServeJSONError("Error: Email exists!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var companyUser models.CompanyUser
|
|
|
|
companyUser.Name = name
|
|
|
|
companyUser.Profile = "{}"
|
|
|
|
companyUser.Role = constants.RoleAdmin
|
|
|
|
|
|
|
|
companyUserId, err := models.AddCompanyUser(o, &companyUser)
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONErrorWithError("Error on saving company user", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var userCompanyMapping models.UserCompanyMap
|
|
|
|
newHash, _ := tokenTools.HashPassword(password)
|
|
|
|
userCompanyMapping.PasswordHash = newHash
|
|
|
|
userCompanyMapping.CompanyUserID = int16(companyUserId)
|
|
|
|
userCompanyMapping.Company = jwtSession.CompanyName
|
|
|
|
userCompanyMapping.Email = email
|
|
|
|
|
|
|
|
_, err = models.AddUserCompanyMap(systemO, &userCompanyMapping)
|
|
|
|
if err == nil {
|
|
|
|
c.ServeJSONSuccess("Success")
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
2018-11-16 18:58:52 +00:00
|
|
|
|
|
|
|
c.ServeJSONErrorWithError("Error on saving user", err)
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOne ...
|
|
|
|
// @Title Get One
|
|
|
|
// @Description get CompanyUser by id
|
|
|
|
// @Param id path string true "The key for staticblock"
|
|
|
|
// @Success 200 {object} models.CompanyUser
|
|
|
|
// @Failure 403 :id is empty
|
|
|
|
// @router /:id [get]
|
|
|
|
func (c *CompanyUserController) GetOne() {
|
|
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
|
|
id, _ := strconv.Atoi(idStr)
|
2018-11-08 07:36:08 +00:00
|
|
|
v, err := models.GetCompanyUserById(o, id)
|
2018-11-06 11:31:49 +00:00
|
|
|
if err != nil {
|
2018-11-16 18:58:52 +00:00
|
|
|
if err.Error() == "<QuerySeter> no row found" {
|
|
|
|
c.ServeJSONError("company user does not exist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.ServeJSONErrorWithError("Error getting company user", err)
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
2018-11-16 18:58:52 +00:00
|
|
|
|
|
|
|
c.Data["json"] = v
|
2018-11-06 11:31:49 +00:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll ...
|
|
|
|
// @Title Get All
|
|
|
|
// @Description get CompanyUser
|
|
|
|
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
|
|
|
|
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
|
|
|
|
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
|
|
|
|
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
|
|
|
|
// @Param limit query string false "Limit the size of result set. Must be an integer"
|
|
|
|
// @Param offset query string false "Start position of result set. Must be an integer"
|
|
|
|
// @Success 200 {object} models.CompanyUser
|
|
|
|
// @Failure 403
|
|
|
|
// @router / [get]
|
|
|
|
func (c *CompanyUserController) GetAll() {
|
|
|
|
var fields []string
|
|
|
|
var sortby []string
|
|
|
|
var order []string
|
|
|
|
var query = make(map[string]string)
|
|
|
|
var limit int64 = 10
|
|
|
|
var offset int64
|
|
|
|
|
|
|
|
// fields: col1,col2,entity.col3
|
|
|
|
if v := c.GetString("fields"); v != "" {
|
|
|
|
fields = strings.Split(v, ",")
|
|
|
|
}
|
|
|
|
// limit: 10 (default is 10)
|
|
|
|
if v, err := c.GetInt64("limit"); err == nil {
|
|
|
|
limit = v
|
|
|
|
}
|
|
|
|
// offset: 0 (default is 0)
|
|
|
|
if v, err := c.GetInt64("offset"); err == nil {
|
|
|
|
offset = v
|
|
|
|
}
|
|
|
|
// sortby: col1,col2
|
|
|
|
if v := c.GetString("sortby"); v != "" {
|
|
|
|
sortby = strings.Split(v, ",")
|
|
|
|
}
|
|
|
|
// order: desc,asc
|
|
|
|
if v := c.GetString("order"); v != "" {
|
|
|
|
order = strings.Split(v, ",")
|
|
|
|
}
|
|
|
|
// query: k:v,k:v
|
|
|
|
if v := c.GetString("query"); v != "" {
|
|
|
|
for _, cond := range strings.Split(v, ",") {
|
|
|
|
kv := strings.SplitN(cond, ":", 2)
|
|
|
|
if len(kv) != 2 {
|
2018-11-16 18:58:52 +00:00
|
|
|
c.ServeJSONError("Error: invalid query key/value pair")
|
2018-11-06 11:31:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
k, v := kv[0], kv[1]
|
|
|
|
query[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 07:36:08 +00:00
|
|
|
l, err := models.GetAllCompanyUser(o, query, fields, sortby, order, offset, limit)
|
2018-11-06 11:31:49 +00:00
|
|
|
if err != nil {
|
2018-11-16 18:58:52 +00:00
|
|
|
c.ServeJSONErrorWithError("Error getting company users", err)
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
2018-11-16 18:58:52 +00:00
|
|
|
c.Data["json"] = l
|
2018-11-06 11:31:49 +00:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put ...
|
|
|
|
// @Title Put
|
|
|
|
// @Description update the CompanyUser
|
|
|
|
// @Param id path string true "The id you want to update"
|
|
|
|
// @Param body body models.CompanyUser true "body for CompanyUser content"
|
|
|
|
// @Success 200 {object} models.CompanyUser
|
|
|
|
// @Failure 403 :id is not int
|
|
|
|
// @router /:id [put]
|
|
|
|
func (c *CompanyUserController) Put() {
|
|
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
|
|
id, _ := strconv.Atoi(idStr)
|
2018-11-16 12:55:28 +00:00
|
|
|
|
|
|
|
if currentUser.Role != constants.RoleAdmin && id != currentUser.Id {
|
|
|
|
c.ServeJSONError("You can only edit your own userdata!")
|
2018-11-16 18:58:52 +00:00
|
|
|
return
|
2018-11-16 12:55:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 11:31:49 +00:00
|
|
|
v := models.CompanyUser{Id: id}
|
2018-11-16 18:58:52 +00:00
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &v)
|
|
|
|
if err == nil {
|
2018-11-16 12:37:18 +00:00
|
|
|
v.ModifiedBy = int64(currentUser.Id)
|
2018-11-08 07:36:08 +00:00
|
|
|
if err := models.UpdateCompanyUserById(o, &v); err == nil {
|
2018-11-16 10:48:45 +00:00
|
|
|
c.ServeJSONSuccess("Ok")
|
2018-11-16 18:58:52 +00:00
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
2018-11-16 18:58:52 +00:00
|
|
|
c.ServeJSONErrorWithError("Error updating company users", err)
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
2018-11-16 18:58:52 +00:00
|
|
|
c.ServeJSONErrorWithError("Error bad format", err)
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete ...
|
|
|
|
// @Title Delete
|
|
|
|
// @Description delete the CompanyUser
|
|
|
|
// @Param id path string true "The id you want to delete"
|
|
|
|
// @Success 200 {string} delete success!
|
|
|
|
// @Failure 403 id is empty
|
|
|
|
// @router /:id [delete]
|
|
|
|
func (c *CompanyUserController) Delete() {
|
|
|
|
idStr := c.Ctx.Input.Param(":id")
|
|
|
|
id, _ := strconv.Atoi(idStr)
|
2018-11-15 17:11:09 +00:00
|
|
|
|
2018-11-15 18:23:53 +00:00
|
|
|
if currentUser.Role != constants.RoleAdmin && currentUser.Id != id {
|
2018-11-15 17:11:09 +00:00
|
|
|
c.ServeJSONError("You can not delete users other than yourself!")
|
2018-11-16 18:58:52 +00:00
|
|
|
return
|
2018-11-15 17:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if currentUser.Role == constants.RoleOwner {
|
|
|
|
c.ServeJSONError("You can not delete users other than yourself!")
|
2018-11-16 18:58:52 +00:00
|
|
|
return
|
2018-11-15 17:11:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 18:23:53 +00:00
|
|
|
uExists, err := models.GetCompanyUserById(o, id)
|
|
|
|
if uExists == nil {
|
|
|
|
c.ServeJSONError("Error: User does not exist!")
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
2018-11-15 17:11:09 +00:00
|
|
|
|
2018-11-15 18:23:53 +00:00
|
|
|
err = models.DeleteCompanyUser(o, id)
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Failed to delete company User")
|
2018-11-16 18:58:52 +00:00
|
|
|
return
|
2018-11-15 18:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
systemDB := companydb.GetSystemDatabase()
|
|
|
|
systemO, err := orm.NewOrmWithDB("postgres", "default", systemDB)
|
2018-11-15 17:11:09 +00:00
|
|
|
|
2018-11-15 18:23:53 +00:00
|
|
|
// After deleting the user here we need to delete the same User in the system DB
|
|
|
|
userCompanyMapping, err := models.GetUserCompanyMapByCompanyAndCID(systemO, jwtSession.CompanyName, int16(id))
|
2018-11-15 17:11:09 +00:00
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Error deleting Company User")
|
|
|
|
return
|
|
|
|
}
|
2018-11-15 18:23:53 +00:00
|
|
|
|
2018-11-15 17:11:09 +00:00
|
|
|
err = models.DeleteUserCompanyMap(systemO, userCompanyMapping.ID)
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Error deleting User Company Relation")
|
|
|
|
return
|
|
|
|
}
|
2018-11-16 18:58:52 +00:00
|
|
|
c.ServeJSONSuccess("Successfully deleted!")
|
|
|
|
return
|
2018-11-06 11:31:49 +00:00
|
|
|
}
|
2018-11-16 08:44:09 +00:00
|
|
|
|
|
|
|
// DeleteCompany ...
|
|
|
|
// @Title Delete Company
|
|
|
|
// @Description Delete the entire Company
|
|
|
|
// @Success 200 {string} delete success!
|
|
|
|
// @Failure 403 failed
|
|
|
|
// @router /deletecompany [delete]
|
|
|
|
func (c *CompanyUserController) DeleteCompany() {
|
|
|
|
|
|
|
|
if currentUser.Role != constants.RoleOwner {
|
|
|
|
c.ServeJSONError("Must be Owner to delete a company")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
systemDB := companydb.GetSystemDatabase()
|
|
|
|
systemO, err := orm.NewOrmWithDB("postgres", "default", systemDB)
|
|
|
|
|
|
|
|
// first check how many users are left
|
|
|
|
ucm, err := models.GetUserCompanyMapsByCompanyName(systemO, jwtSession.CompanyName)
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Error deleting User Company Relation")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uc := range ucm {
|
|
|
|
systemO.Begin()
|
|
|
|
err = models.DeleteUserCompanyMap(systemO, uc.ID)
|
|
|
|
if err != nil {
|
|
|
|
c.ServeJSONError("Error deleting User Company Relation")
|
|
|
|
systemO.Rollback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second check and delete the database
|
|
|
|
err = companydb.DeleteDatabase(jwtSession.CompanyName)
|
|
|
|
if err != nil {
|
|
|
|
systemO.Rollback()
|
|
|
|
c.ServeJSONError("Error deleting Company Database")
|
|
|
|
return
|
|
|
|
}
|
2018-11-16 18:58:52 +00:00
|
|
|
systemO.Commit()
|
|
|
|
c.ServeJSONSuccess("Successfully deleted!")
|
|
|
|
return
|
2018-11-16 08:44:09 +00:00
|
|
|
|
|
|
|
}
|