Esempio n. 1
0
func Migrate() error {
	insertAdmin, err := createUsersTable()
	if err != nil {
		return err
	}

	err = createWindowsUsersTable()
	if err != nil {
		return err
	}

	err = createUsersWindowsUserTable()
	if err != nil {
		return err
	}

	if insertAdmin {
		adminpwd := utils.Env("ADMIN_PASSWORD", "Nanocloud123+")
		adminfirstname := utils.Env("ADMIN_FIRSTNAME", "Admin")
		adminlastname := utils.Env("ADMIN_LASTNAME", "Nanocloud")
		adminmail := utils.Env("ADMIN_MAIL", "*****@*****.**")

		admin, err := users.CreateUser(
			true,
			adminmail,
			adminfirstname,
			adminlastname,
			adminpwd,
			true,
		)

		if err != nil {
			return err
		}

		password := utils.Env("WINDOWS_PASSWORD", "")
		sam := utils.Env("WINDOWS_USER", "")
		domain := utils.Env("WINDOWS_DOMAIN", "")

		err = users.UpdateUserAd(admin.Id, sam, password, domain)

		if err != nil {
			log.Error("Failed to update admin account: ", err)
			return err
		}
	}

	return nil
}
Esempio n. 2
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)
}
Esempio n. 3
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,
		},
	})
}