aboutsummaryrefslogtreecommitdiff
path: root/internal/route/org/setting.go
blob: ab1dd7ffec5f3d14bcc43b2cd5f6b071128d675f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package org

import (
	user2 "gogs.io/gogs/internal/route/user"
	"strings"

	log "gopkg.in/clog.v1"

	"gogs.io/gogs/internal/context"
	"gogs.io/gogs/internal/db"
	"gogs.io/gogs/internal/db/errors"
	"gogs.io/gogs/internal/form"
	"gogs.io/gogs/internal/setting"
)

const (
	SETTINGS_OPTIONS  = "org/settings/options"
	SETTINGS_DELETE   = "org/settings/delete"
	SETTINGS_WEBHOOKS = "org/settings/webhooks"
)

func Settings(c *context.Context) {
	c.Data["Title"] = c.Tr("org.settings")
	c.Data["PageIsSettingsOptions"] = true
	c.HTML(200, SETTINGS_OPTIONS)
}

func SettingsPost(c *context.Context, f form.UpdateOrgSetting) {
	c.Data["Title"] = c.Tr("org.settings")
	c.Data["PageIsSettingsOptions"] = true

	if c.HasError() {
		c.HTML(200, SETTINGS_OPTIONS)
		return
	}

	org := c.Org.Organization

	// Check if organization name has been changed.
	if org.LowerName != strings.ToLower(f.Name) {
		isExist, err := db.IsUserExist(org.ID, f.Name)
		if err != nil {
			c.Handle(500, "IsUserExist", err)
			return
		} else if isExist {
			c.Data["OrgName"] = true
			c.RenderWithErr(c.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &f)
			return
		} else if err = db.ChangeUserName(org, f.Name); err != nil {
			c.Data["OrgName"] = true
			switch {
			case db.IsErrNameReserved(err):
				c.RenderWithErr(c.Tr("user.form.name_reserved"), SETTINGS_OPTIONS, &f)
			case db.IsErrNamePatternNotAllowed(err):
				c.RenderWithErr(c.Tr("user.form.name_pattern_not_allowed"), SETTINGS_OPTIONS, &f)
			default:
				c.Handle(500, "ChangeUserName", err)
			}
			return
		}
		// reset c.org.OrgLink with new name
		c.Org.OrgLink = setting.AppSubURL + "/org/" + f.Name
		log.Trace("Organization name changed: %s -> %s", org.Name, f.Name)
	}
	// In case it's just a case change.
	org.Name = f.Name
	org.LowerName = strings.ToLower(f.Name)

	if c.User.IsAdmin {
		org.MaxRepoCreation = f.MaxRepoCreation
	}

	org.FullName = f.FullName
	org.Description = f.Description
	org.Website = f.Website
	org.Location = f.Location
	if err := db.UpdateUser(org); err != nil {
		c.Handle(500, "UpdateUser", err)
		return
	}
	log.Trace("Organization setting updated: %s", org.Name)
	c.Flash.Success(c.Tr("org.settings.update_setting_success"))
	c.Redirect(c.Org.OrgLink + "/settings")
}

func SettingsAvatar(c *context.Context, f form.Avatar) {
	f.Source = form.AVATAR_LOCAL
	if err := user2.UpdateAvatarSetting(c, f, c.Org.Organization); err != nil {
		c.Flash.Error(err.Error())
	} else {
		c.Flash.Success(c.Tr("org.settings.update_avatar_success"))
	}

	c.Redirect(c.Org.OrgLink + "/settings")
}

func SettingsDeleteAvatar(c *context.Context) {
	if err := c.Org.Organization.DeleteAvatar(); err != nil {
		c.Flash.Error(err.Error())
	}

	c.Redirect(c.Org.OrgLink + "/settings")
}

func SettingsDelete(c *context.Context) {
	c.Title("org.settings")
	c.PageIs("SettingsDelete")

	org := c.Org.Organization
	if c.Req.Method == "POST" {
		if _, err := db.UserLogin(c.User.Name, c.Query("password"), c.User.LoginSource); err != nil {
			if errors.IsUserNotExist(err) {
				c.RenderWithErr(c.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil)
			} else {
				c.ServerError("UserLogin", err)
			}
			return
		}

		if err := db.DeleteOrganization(org); err != nil {
			if db.IsErrUserOwnRepos(err) {
				c.Flash.Error(c.Tr("form.org_still_own_repo"))
				c.Redirect(c.Org.OrgLink + "/settings/delete")
			} else {
				c.ServerError("DeleteOrganization", err)
			}
		} else {
			log.Trace("Organization deleted: %s", org.Name)
			c.Redirect(setting.AppSubURL + "/")
		}
		return
	}

	c.Success(SETTINGS_DELETE)
}

func Webhooks(c *context.Context) {
	c.Data["Title"] = c.Tr("org.settings")
	c.Data["PageIsSettingsHooks"] = true
	c.Data["BaseLink"] = c.Org.OrgLink
	c.Data["Description"] = c.Tr("org.settings.hooks_desc")
	c.Data["Types"] = setting.Webhook.Types

	ws, err := db.GetWebhooksByOrgID(c.Org.Organization.ID)
	if err != nil {
		c.Handle(500, "GetWebhooksByOrgId", err)
		return
	}

	c.Data["Webhooks"] = ws
	c.HTML(200, SETTINGS_WEBHOOKS)
}

func DeleteWebhook(c *context.Context) {
	if err := db.DeleteWebhookOfOrgByID(c.Org.Organization.ID, c.QueryInt64("id")); err != nil {
		c.Flash.Error("DeleteWebhookByOrgID: " + err.Error())
	} else {
		c.Flash.Success(c.Tr("repo.settings.webhook_deletion_success"))
	}

	c.JSON(200, map[string]interface{}{
		"redirect": c.Org.OrgLink + "/settings/hooks",
	})
}