Example #1
0
// Add a new log entry to the database
func Add(c *echo.Context) error {
	history := histories.History{}

	err := utils.ParseJSONBody(c, &history)
	if err != nil {
		return err
	}

	if history.UserId == "" || history.ConnectionId == "" || history.StartDate == "" || history.EndDate == "" {
		log.Error("Missing one or several parameters to create entry")
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "Missing parameters",
				},
			},
		})
	}

	err = utils.ParseJSONBody(c, &history)
	newHistory, err := histories.CreateHistory(
		history.UserId,
		history.ConnectionId,
		history.StartDate,
		history.EndDate,
	)

	if err != nil {
		return err
	}

	return utils.JSON(c, http.StatusCreated, newHistory)
}
Example #2
0
func CreateMachine(c *echo.Context) error {
	rt := &machine{}

	err := utils.ParseJSONBody(c, rt)
	if err != nil {
		log.Error(err.Error())
		return err
	}

	machineType, err := vms.Type(rt.Type)
	if err != nil {
		return errors.MachineTypeNotFound
	}

	attr := vm.MachineAttributes{
		Type:     machineType,
		Name:     rt.Name,
		Username: rt.Username,
		Password: rt.AdminPassword,
		Ip:       rt.Ip,
	}

	m, err := vms.Create(attr)
	if err != nil {
		log.Error(err)
		return errors.UnableToCreateTheMachine
	}

	rt, err = getSerializableMachine(m.Id())
	if err != nil {
		return err
	}

	return utils.JSON(c, http.StatusOK, rt)
}
Example #3
0
func Update(c *echo.Context) error {
	u := users.User{}

	err := utils.ParseJSONBody(c, &u)
	if err != nil {
		return nil
	}

	user, err := users.GetUser(u.GetID())
	if err != nil {
		return apiErrors.UserNotFound
	}

	if u.Password == "" {
		return apiErrors.InvalidRequest.Detail("The password field is missing.")
	}

	err = users.UpdateUserPassword(user.GetID(), u.Password)
	if err != nil {
		log.Error(err)
		return apiErrors.InternalError.Detail("Unable to update the password.")
	}

	return utils.JSON(c, http.StatusOK, user)
}
Example #4
0
func Update(c *echo.Context) error {
	updatedUser := users.User{}
	user := c.Get("user").(*users.User)

	err := utils.ParseJSONBody(c, &updatedUser)
	if err != nil {
		return apiErrors.InvalidRequest
	}

	currentUser, err := users.GetUser(updatedUser.GetID())
	if err != nil {
		return apiErrors.UserNotFound
	}

	if !user.IsAdmin && (updatedUser.GetID() != user.GetID()) {
		return apiErrors.Unauthorized.Detail("You can only update your account")
	}

	if updatedUser.IsAdmin != currentUser.IsAdmin {
		if currentUser.Id == user.GetID() {
			return apiErrors.Unauthorized.Detail("You cannot grant administration rights")
		}
		err = users.UpdateUserPrivilege(updatedUser.GetID(), updatedUser.IsAdmin)
		if err != nil {
			log.Error(err)
			return apiErrors.InternalError.Detail("Unable to update the rank")
		}
	} else if updatedUser.Password != "" {
		err = users.UpdateUserPassword(updatedUser.GetID(), updatedUser.Password)
		if err != nil {
			log.Error(err)
			return apiErrors.InternalError.Detail("Unable to update the password")
		}
	} else if updatedUser.Email != currentUser.Email {
		err = users.UpdateUserEmail(updatedUser.GetID(), updatedUser.Email)
		if err != nil {
			log.Error(err)
			return apiErrors.InternalError.Detail("Unable to update the email")
		}
	} else if updatedUser.FirstName != currentUser.FirstName {
		err = users.UpdateUserFirstName(updatedUser.GetID(), updatedUser.FirstName)
		if err != nil {
			log.Error(err)
			return apiErrors.InternalError.Detail("Unable to update the first name")
		}
	} else if updatedUser.LastName != currentUser.LastName {
		err = users.UpdateUserLastName(updatedUser.GetID(), updatedUser.LastName)
		if err != nil {
			log.Error(err)
			return apiErrors.InternalError.Detail("Unable to update the last name")
		}
	} else {
		return apiErrors.InvalidRequest.Detail("No field sent")
	}

	return utils.JSON(c, http.StatusOK, &updatedUser)
}
Example #5
0
func UpdatePassword(c *echo.Context) error {
	userId := c.Param("id")
	if userId == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "User id needed to modify account",
				},
			},
		})
	}

	var user struct {
		Data struct {
			Password string
		}
	}

	err := utils.ParseJSONBody(c, &user)
	if err != nil {
		return nil
	}

	exists, err := users.UserExists(userId)
	if err != nil {
		log.Errorf("Unable to check user existance: %s", err.Error())
		return err
	}

	if !exists {
		return c.JSON(http.StatusNotFound, hash{
			"error": [1]hash{
				hash{
					"detail": "User not found",
				},
			},
		})
	}

	err = users.UpdateUserPassword(userId, user.Data.Password)
	if err != nil {
		log.Errorf("Unable to update user password: %s", err.Error())
		return err
	}

	return c.JSON(http.StatusOK, hash{
		"data": hash{
			"success": true,
		},
	})
}
Example #6
0
func PublishApplication(c *echo.Context) error {
	app := &apps.Application{}
	err := utils.ParseJSONBody(c, app)
	if err != nil {
		return err
	}

	user := c.Get("user").(*users.User)

	err = apps.PublishApp(user, app)
	if err != nil {
		return err
	}

	return utils.JSON(c, http.StatusOK, app)
}
Example #7
0
func PublishApplication(c *echo.Context) error {
	var params struct {
		Data struct {
			Attributes struct {
				Path string `json:"path"`
			}
		}
	}

	err := utils.ParseJSONBody(c, &params)
	if err != nil {
		return nil
	}

	trimmedpath := strings.TrimSpace(params.Data.Attributes.Path)
	if trimmedpath == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "App path is empty",
				},
			},
		})
	}
	err = apps.PublishApp(trimmedpath)
	if err == apps.PublishFailed {
		return c.JSON(http.StatusInternalServerError, hash{
			"error": [1]hash{
				hash{
					"detail": err,
				},
			},
		})
	}

	return c.JSON(http.StatusOK, hash{
		"data": hash{
			"success": true,
		},
	})
}
Example #8
0
func Post(c *echo.Context) error {
	u := users.User{}

	err := utils.ParseJSONBody(c, &u)
	if err != nil {
		return err
	}

	if u.Email == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "email is missing",
				},
			},
		})
	}

	if u.FirstName == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "first-name is missing",
				},
			},
		})
	}

	if u.LastName == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "last-name is missing",
				},
			},
		})
	}

	if u.Password == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "password is missing",
				},
			},
		})
	}

	newUser, err := users.CreateUser(
		true,
		u.Email,
		u.FirstName,
		u.LastName,
		u.Password,
		false,
	)
	switch err {
	case users.UserDuplicated:
		return c.JSON(http.StatusConflict, hash{
			"error": [1]hash{
				hash{
					"detail": err.Error(),
				},
			},
		})
	case users.UserNotCreated:
		return err
	}

	winpass := utils.RandomString(8) + "s4D+"
	sam, err := ldap.AddUser(newUser.Id, winpass)
	if err != nil {
		return err
	}

	err = users.UpdateUserAd(newUser.Id, sam, winpass, "intra.localdomain.com")
	if err != nil {
		return err
	}

	return utils.JSON(c, http.StatusCreated, newUser)
}
Example #9
0
func Post(c *echo.Context) error {
	var attr hash

	err := utils.ParseJSONBody(c, &attr)
	if err != nil {
		return err
	}

	data, ok := attr["data"].(map[string]interface{})
	if ok == false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "data is missing",
				},
			},
		})
	}

	attributes, ok := data["attributes"].(map[string]interface{})
	if ok == false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "attributes is missing",
				},
			},
		})
	}

	email, ok := attributes["email"].(string)
	if ok == false || email == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "email is missing",
				},
			},
		})
	}

	firstName, ok := attributes["first_name"].(string)
	if ok == false || firstName == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "first_name is missing",
				},
			},
		})
	}

	lastName, ok := attributes["last_name"].(string)
	if ok == false || lastName == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "last_name is missing",
				},
			},
		})
	}

	password, ok := attributes["password"].(string)
	if ok == false || password == "" {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "password is missing",
				},
			},
		})
	}

	newUser, err := users.CreateUser(
		true,
		email,
		firstName,
		lastName,
		password,
		false,
	)
	switch err {
	case users.UserDuplicated:
		return c.JSON(http.StatusConflict, hash{
			"error": [1]hash{
				hash{
					"detail": err.Error(),
				},
			},
		})
	case users.UserNotCreated:
		return err
	}

	winpass := utils.RandomString(8) + "s4D+"
	sam, err := ldap.AddUser(newUser.Id, winpass)
	if err != nil {
		return err
	}

	err = users.UpdateUserAd(newUser.Id, sam, winpass)
	if err != nil {
		return err
	}

	return c.JSON(http.StatusCreated, hash{
		"data": hash{
			"id": newUser.Id,
		},
	})
}
Example #10
0
func Update(c *echo.Context) error {
	var attr map[string]map[string]interface{}

	err := utils.ParseJSONBody(c, &attr)
	if err != nil {
		return nil
	}

	data, ok := attr["data"]
	if ok == false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "data is missing",
				},
			},
		})
	}

	attributes, ok := data["attributes"].(map[string]interface{})
	if ok == false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "attributes is missing",
				},
			},
		})
	}

	activated, ok := attributes["activated"]
	if ok == false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "activated field is missing",
				},
			},
		})
	}

	if activated != false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "activated field must be false",
				},
			},
		})
	}

	code, err := Disable(c.Param("id"))
	if err != nil {
		return c.JSON(code, hash{
			"error": [1]hash{
				hash{
					"detail": err.Error(),
				},
			},
		})
	}

	user, err := users.GetUser(c.Param("id"))
	if user == nil {
		return c.JSON(http.StatusOK, hash{
			"data": hash{
				"success": true,
			},
		})
	}

	return c.JSON(http.StatusOK, hash{
		"data": hash{
			"success":    true,
			"id":         user.Id,
			"type":       "user",
			"attributes": user,
		},
	})
}
Example #11
0
// Add a new log entry to the database
func Add(c *echo.Context) error {
	var attr hash

	err := utils.ParseJSONBody(c, &attr)
	if err != nil {
		return err
	}

	data, ok := attr["data"].(map[string]interface{})
	if ok == false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "data is missing",
				},
			},
		})
	}

	attributes, ok := data["attributes"].(map[string]interface{})
	if ok == false {
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "attributes is missing",
				},
			},
		})
	}

	user_id, ok := attributes["user_id"].(string)
	connection_id, ok := attributes["connection_id"].(string)
	start_date, ok := attributes["start_date"].(string)
	end_date, ok := attributes["end_date"].(string)
	if user_id == "" || connection_id == "" || start_date == "" || end_date == "" {
		log.Error("Missing one or several parameters to create entry")
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "Missing parameters",
				},
			},
		})
	}

	rows, err := db.Query(
		`INSERT INTO histories
		(userid, connectionid, startdate, enddate)
		VALUES(	$1::varchar, $2::varchar, $3::varchar, $4::varchar)
		`, user_id, connection_id, start_date, end_date)
	if err != nil {
		return err
	}

	rows.Close()

	return c.JSON(http.StatusCreated, hash{
		"data": hash{
			"attributes": hash{
				"user_id":       user_id,
				"connection_id": connection_id,
				"start_date":    start_date,
				"end_date":      end_date,
			},
			"type": "history",
			"id":   0,
		},
	})
}
Example #12
0
func PatchMachine(c *echo.Context) error {
	b := &machine{}

	err := utils.ParseJSONBody(c, b)
	if err != nil {
		log.Error(err)
		return errors.UnableToUpdateMachineStatus
	}

	m, err := vms.Machine(b.Id)
	if err != nil {
		log.Error(err)
		return errors.UnableToUpdateMachineStatus
	}

	status, err := m.Status()
	if err != nil {
		log.Error(err)
		return errors.UnableToUpdateMachineStatus
	}

	switch b.Status {
	case "up":
		if status != vms.StatusDown {
			return errors.UnableToUpdateMachineStatus
		}

		err = m.Start()
		if err != nil {
			log.Error(err)
			return errors.UnableToUpdateMachineStatus
		}

	case "down":
		if status != vms.StatusUp {
			return errors.UnableToUpdateMachineStatus
		}
		err = m.Stop()
		if err != nil {
			log.Error(err)
			return errors.UnableToUpdateMachineStatus
		}

	default:
		log.Error(err)
		return errors.UnableToUpdateMachineStatus
	}

	m, err = vms.Machine(m.Id())
	if err != nil {
		log.Error(err)
		return errors.UnableToUpdateMachineStatus
	}

	rt, err := getSerializableMachine(m.Id())
	if err != nil {
		log.Error(err)
		return errors.UnableToUpdateMachineStatus
	}

	return utils.JSON(c, http.StatusOK, rt)
}