Example #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
}
Example #2
0
func init() {
	new_user, err := users.CreateUser(
		true,
		"*****@*****.**",
		"Test",
		"user",
		"secret",
		false,
	)

	if err != nil {
		log.Panicln("Can't create new account:", err.Error())
	}
	if new_user == nil {
		log.Panicln("Can't create new account")
	}
	user = new_user
}
Example #3
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 #4
0
func Migrate() error {
	rows, err := db.Query(
		`SELECT table_name
			FROM information_schema.tables
			WHERE table_name = 'users'`)
	if err != nil {
		return err
	}
	defer rows.Close()

	if rows.Next() {
		return nil
	}

	rows, err = db.Query(
		`CREATE TABLE users (
				id               varchar(36) PRIMARY KEY,
				first_name       varchar(36) NOT NULL DEFAULT '',
				last_name        varchar(36) NOT NULL DEFAULT '',
				email            varchar(36) NOT NULL DEFAULT '' UNIQUE,
				password         varchar(60) NOT NULL DEFAULT '',
				is_admin         boolean,
				activated        boolean,
				sam              varchar(35) NOT NULL DEFAULT '',
				windows_password varchar(36) NOT NULL DEFAULT ''
			);`)
	if err != nil {
		return err
	}

	rows.Close()

	admin, err := users.CreateUser(
		true,
		"*****@*****.**",
		"John",
		"Doe",
		"admin",
		true,
	)

	if err != nil {
		return err
	}

	password := utils.Env("WIN_PASSWORD", "")
	sam := utils.Env("WIN_USER", "")

	result, err := db.Exec(
		`UPDATE users
				SET sam = $1::varchar,
				windows_password = $2::varchar
				WHERE id = $3::varchar;`,
		sam,
		password,
		admin.Id,
	)

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

	updated, err := result.RowsAffected()
	if err != nil {
		log.Error(err)
		return err
	}

	if updated != 1 {
		return errors.New("Unable to set admin password")
	}
	return nil
}
Example #5
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,
		},
	})
}