1// Copyright 2015 The Gogs Authors. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package user
6
7import (
8	"fmt"
9	"net/http"
10
11	user_model "code.gitea.io/gitea/models/user"
12	"code.gitea.io/gitea/modules/context"
13	"code.gitea.io/gitea/modules/convert"
14	"code.gitea.io/gitea/modules/setting"
15	api "code.gitea.io/gitea/modules/structs"
16	"code.gitea.io/gitea/modules/web"
17)
18
19// ListEmails list all of the authenticated user's email addresses
20// see https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user
21func ListEmails(ctx *context.APIContext) {
22	// swagger:operation GET /user/emails user userListEmails
23	// ---
24	// summary: List the authenticated user's email addresses
25	// produces:
26	// - application/json
27	// responses:
28	//   "200":
29	//     "$ref": "#/responses/EmailList"
30
31	emails, err := user_model.GetEmailAddresses(ctx.User.ID)
32	if err != nil {
33		ctx.Error(http.StatusInternalServerError, "GetEmailAddresses", err)
34		return
35	}
36	apiEmails := make([]*api.Email, len(emails))
37	for i := range emails {
38		apiEmails[i] = convert.ToEmail(emails[i])
39	}
40	ctx.JSON(http.StatusOK, &apiEmails)
41}
42
43// AddEmail add an email address
44func AddEmail(ctx *context.APIContext) {
45	// swagger:operation POST /user/emails user userAddEmail
46	// ---
47	// summary: Add email addresses
48	// produces:
49	// - application/json
50	// parameters:
51	// - name: options
52	//   in: body
53	//   schema:
54	//     "$ref": "#/definitions/CreateEmailOption"
55	// parameters:
56	// - name: body
57	//   in: body
58	//   schema:
59	//     "$ref": "#/definitions/CreateEmailOption"
60	// responses:
61	//   '201':
62	//     "$ref": "#/responses/EmailList"
63	//   "422":
64	//     "$ref": "#/responses/validationError"
65	form := web.GetForm(ctx).(*api.CreateEmailOption)
66	if len(form.Emails) == 0 {
67		ctx.Error(http.StatusUnprocessableEntity, "", "Email list empty")
68		return
69	}
70
71	emails := make([]*user_model.EmailAddress, len(form.Emails))
72	for i := range form.Emails {
73		emails[i] = &user_model.EmailAddress{
74			UID:         ctx.User.ID,
75			Email:       form.Emails[i],
76			IsActivated: !setting.Service.RegisterEmailConfirm,
77		}
78	}
79
80	if err := user_model.AddEmailAddresses(emails); err != nil {
81		if user_model.IsErrEmailAlreadyUsed(err) {
82			ctx.Error(http.StatusUnprocessableEntity, "", "Email address has been used: "+err.(user_model.ErrEmailAlreadyUsed).Email)
83		} else if user_model.IsErrEmailCharIsNotSupported(err) ||
84			user_model.IsErrEmailInvalid(err) {
85			errMsg := fmt.Sprintf("Email address %s invalid", err.(user_model.ErrEmailInvalid).Email)
86			ctx.Error(http.StatusUnprocessableEntity, "", errMsg)
87		} else {
88			ctx.Error(http.StatusInternalServerError, "AddEmailAddresses", err)
89		}
90		return
91	}
92
93	apiEmails := make([]*api.Email, len(emails))
94	for i := range emails {
95		apiEmails[i] = convert.ToEmail(emails[i])
96	}
97	ctx.JSON(http.StatusCreated, &apiEmails)
98}
99
100// DeleteEmail delete email
101func DeleteEmail(ctx *context.APIContext) {
102	// swagger:operation DELETE /user/emails user userDeleteEmail
103	// ---
104	// summary: Delete email addresses
105	// produces:
106	// - application/json
107	// parameters:
108	// - name: body
109	//   in: body
110	//   schema:
111	//     "$ref": "#/definitions/DeleteEmailOption"
112	// responses:
113	//   "204":
114	//     "$ref": "#/responses/empty"
115	//   "404":
116	//     "$ref": "#/responses/notFound"
117	form := web.GetForm(ctx).(*api.DeleteEmailOption)
118	if len(form.Emails) == 0 {
119		ctx.Status(http.StatusNoContent)
120		return
121	}
122
123	emails := make([]*user_model.EmailAddress, len(form.Emails))
124	for i := range form.Emails {
125		emails[i] = &user_model.EmailAddress{
126			Email: form.Emails[i],
127			UID:   ctx.User.ID,
128		}
129	}
130
131	if err := user_model.DeleteEmailAddresses(emails); err != nil {
132		if user_model.IsErrEmailAddressNotExist(err) {
133			ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err)
134			return
135		}
136		ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
137		return
138	}
139	ctx.Status(http.StatusNoContent)
140}
141