Example #1
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 #2
0
func GetUser(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 retrieve account informations",
				},
			},
		})
	}

	user, err := users.GetUser(userId)
	if err != nil {
		return err
	}

	if user == nil {
		return c.JSON(http.StatusNotFound, hash{
			"error": [1]hash{
				hash{
					"detail": "User Not Found",
				},
			},
		})
	}

	return utils.JSON(c, http.StatusOK, user)
}
Example #3
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 #4
0
func List(c *echo.Context) error {

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

	winUser, err := user.WindowsCredentials()
	if err != nil {
		return err
	}

	sessionList, err := sessions.GetAll(winUser.Sam)

	if err != nil {
		log.Error(err)
		return utils.JSON(c, http.StatusInternalServerError, hash{
			"error": [1]hash{
				hash{
					"detail": err.Error(),
				},
			},
		})
	}

	var response = make([]hash, len(sessionList))
	for i, val := range sessionList {
		res := hash{
			"id":         val.Id,
			"type":       "session",
			"attributes": val,
		}
		response[i] = res
	}

	return c.JSON(http.StatusOK, hash{"data": response})
}
func FindAll(c *echo.Context) error {
	drivers, err := machinedrivers.FindAll()
	if err != nil {
		return err
	}
	return utils.JSON(c, http.StatusOK, drivers)
}
Example #6
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 #7
0
// Get a list of all the log entries of the database
func List(c *echo.Context) error {

	histories, err := histories.FindAll()
	if err != nil {
		return err
	}
	return utils.JSON(c, http.StatusOK, histories)
}
Example #8
0
func GetMachine(c *echo.Context) error {
	m, err := getSerializableMachine(c.Param("id"))
	if err != nil {
		return err
	}

	return utils.JSON(c, http.StatusOK, m)
}
Example #9
0
func Get(c *echo.Context) error {
	user := c.Get("user").(*users.User)
	if c.Query("me") == "true" {
		return utils.JSON(c, http.StatusOK, user)
	}

	if !user.IsAdmin {
		return apiErrors.AdminLevelRequired
	}

	users, err := users.FindUsers()
	if err != nil {
		log.Error(err)
		return apiErrors.InternalError.Detail("Unable to retreive the user list")
	}

	return utils.JSON(c, http.StatusOK, users)
}
Example #10
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 #11
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 #12
0
func Machines(c *echo.Context) error {
	machines, err := vms.Machines()
	if err != nil {
		log.Error(err)
		return errors.UnableToRetrieveMachineList
	}

	res := make([]*machine, len(machines))

	for i, val := range machines {
		m := machine{}
		m.Name, err = val.Name()
		if err != nil {
			log.Error(err)
			return errors.UnableToRetrieveMachineList
		}

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

		m.Platform = val.Platform()

		m.Status = vm.StatusToString(status)

		progress, err := val.Progress()
		if err != nil {
			log.Errorf("Unable to get machine progress: %s", err)
		} else {
			m.Progress = int(progress)
		}

		ip, _ := val.IP()
		if ip != nil {
			m.Ip = ip.String()
		}

		res[i] = &m
	}

	return utils.JSON(c, http.StatusOK, res)
}
Example #13
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 #14
0
func ChangeAppName(c *echo.Context) error {
	appId := c.Param("app_id")
	if len(appId) < 1 {
		return c.JSON(http.StatusBadRequest, hash{
			"error": "App id must be specified",
		})
	}
	var Name struct {
		Data struct {
			Attributes struct {
				DisplayName string `json:"display-name"`
			}
		}
	}

	body, err := ioutil.ReadAll(c.Request().Body)
	if err != nil {
		return err
	}

	err = json.Unmarshal(body, &Name)
	if err != nil {
		log.Errorf("Unable to parse body request: %s", err.Error())
		return err
	}
	if len(Name.Data.Attributes.DisplayName) < 1 {
		log.Errorf("No name provided")
		return c.JSON(http.StatusBadRequest, hash{
			"error": [1]hash{
				hash{
					"detail": "No name provided",
				},
			},
		})
	}

	exists, err := apps.AppExists(appId)
	if err != nil {
		log.Errorf("Unable to check app existence: %s", err.Error())
		return err
	}

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

	err = apps.ChangeName(appId, Name.Data.Attributes.DisplayName)
	if err == apps.FailedNameChange {
		return c.JSON(http.StatusInternalServerError, hash{
			"error": [1]hash{
				hash{
					"detail": err.Error(),
				},
			},
		})
	}

	application, err := apps.GetApp(appId)

	if application == nil {
		return c.JSON(http.StatusNotFound, hash{
			"error": [1]hash{
				hash{
					"detail": "Application Not Found",
				},
			},
		})
	}

	return utils.JSON(c, http.StatusOK, application)
}
Example #15
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)
}