(user *types.Account)
| 11 | ) |
| 12 | |
| 13 | func CreateUser(user *types.Account) error { |
| 14 | if exists, _ := CheckIfUsernameExists(user.Username); exists { |
| 15 | return fmt.Errorf("username already exists") |
| 16 | } |
| 17 | |
| 18 | tx, err := database.DB.Begin() |
| 19 | if err != nil { |
| 20 | log.Fatal(err) |
| 21 | return fmt.Errorf("Begin transaction error: %w", err) |
| 22 | } |
| 23 | if exists, _ := CheckIfUsernameExists(user.Username); exists { |
| 24 | return fmt.Errorf("username already exists") |
| 25 | } |
| 26 | stmt, err := tx.Prepare(` |
| 27 | INSERT INTO users( |
| 28 | username, |
| 29 | password, |
| 30 | email, |
| 31 | scope, |
| 32 | read_directories, |
| 33 | read_files, |
| 34 | create_permission, |
| 35 | change_permission, |
| 36 | delete_permission, |
| 37 | move_permission, |
| 38 | download_permission, |
| 39 | upload_permission, |
| 40 | rename_permission, |
| 41 | extract_permission, |
| 42 | archive_permission, |
| 43 | copy_permission, |
| 44 | logs_permission, |
| 45 | read_recovery_permission, |
| 46 | use_recovery_permission, |
| 47 | read_users_permission, |
| 48 | edit_users_permission |
| 49 | ) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 50 | `) |
| 51 | |
| 52 | if err != nil { |
| 53 | return fmt.Errorf("error creating db stmt") |
| 54 | } |
| 55 | |
| 56 | defer stmt.Close() |
| 57 | |
| 58 | hash, err := utils.Hash([]byte(user.Password)) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | hashString := hex.EncodeToString(hash[:]) |
| 64 | |
| 65 | _, err = stmt.Exec( |
| 66 | user.Username, |
| 67 | hashString, |
| 68 | user.Email, |
| 69 | user.Scope, |
| 70 | user.Permissions.ReadDirectories, |
no test coverage detected