(c *fiber.Ctx)
| 10 | ) |
| 11 | |
| 12 | func CreateUser(c *fiber.Ctx) error { |
| 13 | if !c.Locals("account").(types.Account).Permissions.EditUsers { |
| 14 | return c.Status(403).JSON( |
| 15 | fiber.Map{"err": "No permission!"}, |
| 16 | ) |
| 17 | } |
| 18 | |
| 19 | var requestBody struct { |
| 20 | User types.Account `json:"user"` |
| 21 | } |
| 22 | |
| 23 | if err := c.BodyParser(&requestBody); err != nil { |
| 24 | return c.Status(400).JSON( |
| 25 | fiber.Map{"err": "Bad request! " + err.Error()}, |
| 26 | ) |
| 27 | } |
| 28 | |
| 29 | if requestBody.User.Username == "" { |
| 30 | return c.Status(400).JSON( |
| 31 | fiber.Map{"err": "Username is missing."}, |
| 32 | ) |
| 33 | } |
| 34 | |
| 35 | if requestBody.User.Password == "" { |
| 36 | return c.Status(400).JSON( |
| 37 | fiber.Map{"err": "Password is missing."}, |
| 38 | ) |
| 39 | } |
| 40 | |
| 41 | err := users.CreateUser(&requestBody.User) |
| 42 | |
| 43 | if err != nil { |
| 44 | if err.Error() == "username already exists" { |
| 45 | return c.Status(400).JSON( |
| 46 | fiber.Map{"err": "Username already exists."}, |
| 47 | ) |
| 48 | } |
| 49 | return c.Status(500).JSON( |
| 50 | fiber.Map{"err": "Unknown server error."}, |
| 51 | ) |
| 52 | } |
| 53 | |
| 54 | logs.CreateLog(types.AuditLog{ |
| 55 | Username: c.Locals("account").(types.Account).Username, |
| 56 | Action: "Create user", |
| 57 | Description: fmt.Sprintf("%s created a new user %s", c.Locals("account").(types.Account).Username, requestBody.User.Username), |
| 58 | }) |
| 59 | |
| 60 | return c.Status(200).JSON( |
| 61 | fiber.Map{"response": "User successfully created!"}, |
| 62 | ) |
| 63 | } |
nothing calls this directly
no test coverage detected