// Save the configuration key. If the value exists already, it will be overwritten. func Set(key, value string) { _, err := db.Exec( `INSERT INTO config (key, value) VALUES($1::varchar, $2::varchar) ON CONFLICT(key) DO UPDATE SET value = excluded.value`, key, value, ) if err != nil { log.Error(err) } }
func Post(c *echo.Context) error { user := c.Get("user").(*users.User) req := c.Request() accessToken, _ := oauth2.GetAuthorizationHeaderValue(req, "Bearer") _, err := db.Exec( `DELETE FROM oauth_access_tokens WHERE token = $1::varchar AND user_id = $2::varchar`, accessToken, user.Id, ) return err }
func (m *machine) Terminate() error { res, err := db.Exec("DELETE FROM machines WHERE id = $1::varchar", m.id) if err != nil { return err } deleted, err := res.RowsAffected() if err != nil { return err } if deleted == 0 { return errors.New("machine entry not found") } return nil }
func DeleteUser(id string) error { res, err := db.Exec("DELETE FROM users WHERE id = $1::varchar", id) if err != nil { return err } deleted, err := res.RowsAffected() if err != nil { return err } if deleted == 0 { return UserNotFound } return nil }
func (app *App) Delete() error { res, err := db.Exec(`DELETE FROM apps where id=$1::varchar`, app.Id) if err != nil { return err } deleted, err := res.RowsAffected() if err != nil { return err } if deleted == 0 { return AppNotFound } return nil }
func (c oauthConnector) RevokeAccessToken(rawClient interface{}, rawUser interface{}, accessToken string) error { user := rawUser.(*users.User) client := rawClient.(*Client) _, err := db.Exec( `DELETE FROM oauth_access_tokens WHERE user_id = $1::varchar AND token = $2::varchar AND oauth_client_id = $3::integer`, user.Id, accessToken, client.Id, ) if err != nil { return err } return nil }
// Remove the specified configuration keys. func Unset(keys ...string) { l := len(keys) queryArgs := make([]string, l) args := make([]interface{}, l) for k, v := range keys { args[k] = v queryArgs[k] = fmt.Sprintf("$%d::varchar", k+1) } _, err := db.Exec( fmt.Sprintf("DELETE FROM config WHERE key IN(%s)", strings.Join(queryArgs, ",")), args..., ) if err != nil { log.Error(err) } }
func UpdateUserAd(userID, sam, password, domain string) error { res, err := db.Query( `INSERT INTO windows_users (sam, password, domain) VALUES ($1::varchar, $2::varchar, $3::varchar) RETURNING id`, sam, password, domain, ) if err != nil { log.Error(err) return UserNotCreated } defer res.Close() if !res.Next() { return UserNotCreated } winUserID := 0 res.Scan(&winUserID) insert, err := db.Exec( `INSERT INTO users_windows_user (user_id, windows_user_id) VALUES ($1::varchar, $2::integer) ON CONFLICT (user_id) DO UPDATE SET windows_user_id = EXCLUDED.windows_user_id`, userID, winUserID, ) if err != nil { log.Error(err) return UserNotCreated } inserted, err := insert.RowsAffected() if err != nil { return err } if inserted == 0 { return UserNotCreated } return nil }
func UpdateUserPrivilege(id string, rank bool) error { res, err := db.Exec( `UPDATE users SET is_admin = $1::boolean WHERE id = $2::varchar`, rank, id) if err != nil { return err } updated, err := res.RowsAffected() if err != nil { return err } if updated == 0 { return UserNotFound } return nil }
func UpdateUserAd(id, sam, password string) error { res, err := db.Exec( `UPDATE users SET sam = $1::varchar, windows_password = $2::varchar WHERE id = $3::varchar`, sam, password, id) if err != nil { return err } updated, err := res.RowsAffected() if err != nil { return err } if updated == 0 { return UserNotFound } return nil }
func UpdateUserLastName(id string, lastname string) error { res, err := db.Exec( `UPDATE users SET last_name = $1::varchar WHERE id = $2::varchar`, lastname, id) if err != nil { return err } updated, err := res.RowsAffected() if err != nil { return err } if updated == 0 { return UserNotFound } return nil }
func UpdateUserEmail(id string, email string) error { res, err := db.Exec( `UPDATE users SET email = $1::varchar WHERE id = $2::varchar`, email, id) if err != nil { return err } updated, err := res.RowsAffected() if err != nil { return err } if updated == 0 { return UserNotFound } return nil }
func Delete(c *echo.Context) error { tokenId := c.Param("id") if len(tokenId) == 4 { return c.JSON(http.StatusBadRequest, hash{ "error": "Invalid token id", }) } user := c.Get("user").(*users.User) fmt.Println(user.Id) fmt.Println(tokenId) res, err := db.Exec( `DELETE FROM oauth_access_tokens WHERE user_id = $1::varchar AND id = $2::varchar`, user.Id, tokenId, ) if err != nil { return err } affected, err := res.RowsAffected() if err != nil { return err } if affected < 1 { return c.JSON(http.StatusNotFound, hash{ "error": "No such token", }) } return c.JSON(http.StatusOK, hash{}) }
func UpdateUserPassword(id string, password string) error { pass, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { return err } res, err := db.Exec( `UPDATE users SET password = $1::varchar WHERE id = $2::varchar`, pass, id) if err != nil { return err } updated, err := res.RowsAffected() if err != nil { return err } if updated == 0 { return UserNotFound } return nil }
func removeExpiredTokens() { db.Exec( `DELETE FROM oauth_access_tokens WHERE expires_at < NOW()`, ) }
func Migrate() error { rows, err := db.Query( `SELECT table_name FROM information_schema.tables WHERE table_name = 'machines'`) if err != nil { log.Error(err.Error()) panic(err) } defer rows.Close() if rows.Next() { log.Info("Machines table already set up") return nil } _, err = db.Exec(`CREATE TYPE vmtype AS ENUM('qemu', 'manual', 'aws', 'vmware', 'azure')`) if err != nil { log.Error(err.Error()) return err } rows, err = db.Query( `CREATE TABLE machines ( id varchar(60), name varchar(255), type vmtype, ip varchar(255), plazaport varchar(4) NOT NULL DEFAULT '9090', username varchar(36), password varchar(60) );`) if err != nil { log.Errorf("Unable to create machines table: %s", err) return err } if os.Getenv("IAAS") == "manual" { servers := os.Getenv("EXECUTION_SERVERS") password := os.Getenv("WINDOWS_PASSWORD") user := os.Getenv("WINDOWS_USER") ips := strings.Split(servers, ";") for i, val := range ips { rows, err := db.Query(`INSERT INTO machines (id, name, type, ip, username, password) VALUES( $1::varchar, $2::varchar, $3::vmtype, $4::varchar, $5::varchar, $6::varchar)`, uuid.NewV4().String(), fmt.Sprintf("Machine #%d", i+1), "manual", val, user, password, ) if err != nil { return err } rows.Close() } } rows.Close() return nil }
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 }