From 57bc2d1ca0bfc3ba90e6d85309dba39415c6db73 Mon Sep 17 00:00:00 2001
From: Unknown <joe2010xtmf@163.com>
Date: Thu, 13 Mar 2014 03:39:18 -0400
Subject: [PATCH] Add update user profile back end, add new gitignore and
 license, add template data to public profile page

---
 conf/app.ini                        |  4 +--
 conf/gitignore/C                    | 18 ++++++++++++
 conf/license/BSD (3-Clause) License | 27 +++++++++++++++++
 gogs.go                             |  2 +-
 models/repo.go                      |  4 +++
 models/user.go                      |  4 ++-
 modules/auth/auth.go                |  5 ----
 modules/auth/user.go                | 45 +++++++++++++++++++++++++++++
 routers/repo/repo.go                |  4 +--
 routers/user/setting.go             | 24 ++++++++++++++-
 routers/user/user.go                |  3 +-
 templates/user/profile.tmpl         | 12 ++++----
 web.go                              |  2 +-
 13 files changed, 133 insertions(+), 21 deletions(-)
 create mode 100644 conf/gitignore/C
 create mode 100644 conf/license/BSD (3-Clause) License

diff --git a/conf/app.ini b/conf/app.ini
index fcfbe9bc2d..aa42129cce 100644
--- a/conf/app.ini
+++ b/conf/app.ini
@@ -3,8 +3,8 @@ RUN_USER = lunny
 
 [repository]
 ROOT = /Users/lunny/git/gogs-repositories
-LANG_IGNS=Google Go
-LICENSES=Apache v2 License
+LANG_IGNS=Google Go|C
+LICENSES=Apache v2 License|BSD (3-Clause) License
 
 [server]
 HTTP_ADDR = 
diff --git a/conf/gitignore/C b/conf/gitignore/C
new file mode 100644
index 0000000000..e7a909e4c6
--- /dev/null
+++ b/conf/gitignore/C
@@ -0,0 +1,18 @@
+# Object files
+*.o
+*.ko
+
+# Libraries
+*.lib
+*.a
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
\ No newline at end of file
diff --git a/conf/license/BSD (3-Clause) License b/conf/license/BSD (3-Clause) License
new file mode 100644
index 0000000000..3af16b0724
--- /dev/null
+++ b/conf/license/BSD (3-Clause) License	
@@ -0,0 +1,27 @@
+Copyright (c) 2014
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+  this list of conditions and the following disclaimer in the documentation
+  and/or other materials provided with the distribution.
+
+* Neither the name of the {organization} nor the names of its
+  contributors may be used to endorse or promote products derived from
+  this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
diff --git a/gogs.go b/gogs.go
index f1a702ed22..96c0f84f2b 100644
--- a/gogs.go
+++ b/gogs.go
@@ -20,7 +20,7 @@ import (
 // Test that go1.1 tag above is included in builds. main.go refers to this definition.
 const go11tag = true
 
-const APP_VER = "0.0.6.0313"
+const APP_VER = "0.0.7.0313"
 
 func init() {
 	runtime.GOMAXPROCS(runtime.NumCPU())
diff --git a/models/repo.go b/models/repo.go
index 6e45367c09..b7b5ac42b0 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -352,6 +352,10 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) {
 		session.Rollback()
 		return err
 	}
+	if _, err := session.Delete(&Access{UserName: userName, RepoName: repo.Name}); err != nil {
+		session.Rollback()
+		return err
+	}
 	if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", userId); err != nil {
 		session.Rollback()
 		return err
diff --git a/models/user.go b/models/user.go
index c59e4ae152..06db7d8ba2 100644
--- a/models/user.go
+++ b/models/user.go
@@ -48,7 +48,9 @@ type User struct {
 	NumFollowings int
 	NumStars      int
 	NumRepos      int
-	Avatar        string    `xorm:"varchar(2048) not null"`
+	Avatar        string `xorm:"varchar(2048) not null"`
+	Location      string
+	Website       string
 	Created       time.Time `xorm:"created"`
 	Updated       time.Time `xorm:"updated"`
 }
diff --git a/modules/auth/auth.go b/modules/auth/auth.go
index e4748650c2..b085527554 100644
--- a/modules/auth/auth.go
+++ b/modules/auth/auth.go
@@ -90,11 +90,6 @@ func (f *LogInForm) Validate(errors *binding.Errors, req *http.Request, context
 	validate(errors, data, f)
 }
 
-type FeedsForm struct {
-	UserId int64 `form:"userid" binding:"Required"`
-	Offset int64 `form:"offset"`
-}
-
 func getMinMaxSize(field reflect.StructField) string {
 	for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
 		if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
diff --git a/modules/auth/user.go b/modules/auth/user.go
index 4059edfdd3..e868fac23d 100644
--- a/modules/auth/user.go
+++ b/modules/auth/user.go
@@ -5,10 +5,15 @@
 package auth
 
 import (
+	"net/http"
+	"reflect"
+
 	"github.com/codegangsta/martini"
 	"github.com/martini-contrib/render"
 	"github.com/martini-contrib/sessions"
 
+	"github.com/gogits/binding"
+
 	"github.com/gogits/gogs/models"
 	"github.com/gogits/gogs/modules/base"
 	"github.com/gogits/gogs/modules/log"
@@ -83,3 +88,43 @@ func SignOutRequire() martini.Handler {
 		}
 	}
 }
+
+type FeedsForm struct {
+	UserId int64 `form:"userid" binding:"Required"`
+	Offset int64 `form:"offset"`
+}
+
+type UpdateProfileForm struct {
+	Email    string `form:"email" binding:"Required;Email;MaxSize(50)"`
+	Website  string `form:"website" binding:"AlphaDash;MaxSize(50)"`
+	Location string `form:"location" binding:"MaxSize(50)"`
+	Avatar   string `form:"avatar" binding:"Required;Email;MaxSize(50)"`
+}
+
+func (f *UpdateProfileForm) Name(field string) string {
+	names := map[string]string{
+		"Email":    "Email address",
+		"Website":  "Website",
+		"Location": "Location",
+		"Avatar":   "Gravatar Email",
+	}
+	return names[field]
+}
+
+func (f *UpdateProfileForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
+	if req.Method == "GET" || errors.Count() == 0 {
+		return
+	}
+
+	data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
+	data["HasError"] = true
+
+	if len(errors.Overall) > 0 {
+		for _, err := range errors.Overall {
+			log.Error("UpdateProfileForm.Validate: %v", err)
+		}
+		return
+	}
+
+	validate(errors, data, f)
+}
diff --git a/routers/repo/repo.go b/routers/repo/repo.go
index a12b45d7b9..e1809b4b7e 100644
--- a/routers/repo/repo.go
+++ b/routers/repo/repo.go
@@ -46,7 +46,7 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
 	if err == nil {
 		if _, err = models.CreateRepository(user,
 			form.RepoName, form.Description, form.Language, form.License,
-			form.Visibility == "private", form.InitReadme == "true"); err == nil {
+			form.Visibility == "private", form.InitReadme == "on"); err == nil {
 			if err == nil {
 				data["RepoName"] = user.Name + "/" + form.RepoName
 				r.HTML(200, "repo/created", data)
@@ -83,7 +83,7 @@ func Delete(form auth.DeleteRepoForm, req *http.Request, r render.Render, data b
 		return
 	}
 
-	r.Redirect("/", 200)
+	r.Redirect("/", 302)
 }
 
 func List(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
diff --git a/routers/user/setting.go b/routers/user/setting.go
index 0669784be0..63e4d8cb7d 100644
--- a/routers/user/setting.go
+++ b/routers/user/setting.go
@@ -17,9 +17,31 @@ import (
 	"github.com/gogits/gogs/modules/log"
 )
 
-func Setting(r render.Render, data base.TmplData, session sessions.Session) {
+func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) {
 	data["Title"] = "Setting"
 	data["PageIsUserSetting"] = true
+
+	user := auth.SignedInUser(session)
+	if req.Method == "GET" {
+		data["Owner"] = user
+	}
+
+	if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
+		r.HTML(200, "user/setting", data)
+		return
+	}
+
+	user.Email = form.Email
+	user.Website = form.Website
+	user.Location = form.Location
+	user.Avatar = base.EncodeMd5(form.Avatar)
+	if err := models.UpdateUser(user); err != nil {
+		data["ErrorMsg"] = err
+		log.Error("setting.Setting: %v", err)
+		r.HTML(200, "base/error", data)
+		return
+	}
+
 	r.HTML(200, "user/setting", data)
 }
 
diff --git a/routers/user/user.go b/routers/user/user.go
index 59177a47b9..2ce158b641 100644
--- a/routers/user/user.go
+++ b/routers/user/user.go
@@ -43,8 +43,7 @@ func Profile(params martini.Params, r render.Render, data base.TmplData, session
 		return
 	}
 
-	data["Avatar"] = user.Avatar
-	data["Username"] = user.Name
+	data["Owner"] = user
 	r.HTML(200, "user/profile", data)
 }
 
diff --git a/templates/user/profile.tmpl b/templates/user/profile.tmpl
index 79e6062bcb..94ec33b696 100644
--- a/templates/user/profile.tmpl
+++ b/templates/user/profile.tmpl
@@ -4,16 +4,16 @@
     <div id="gogs-user-profile" class="col-md-3">
         <div class="profile-avatar text-center">
             <a href="#" class="center-block" data-toggle="tooltip" data-placement="bottom" title="Change Avatar">
-                <img id="gogs-user-avatar" src="http://1.gravatar.com/avatar/{{.Avatar}}?s=200" alt="user-avatar" title="username"/>
+                <img id="gogs-user-avatar" src="http://1.gravatar.com/avatar/{{.Owner.Avatar}}?s=200" alt="user-avatar" title="username"/>
             </a>
-            <span id="gogs-user-name" class="center-block" href="#">{{.Username}}</span>
+            <span id="gogs-user-name" class="center-block" href="#">{{.Owner.Name}}</span>
         </div>
         <div class="profile-info">
             <ul class="list-group">
-                <li class="list-group-item"><i class="fa fa-thumb-tack"></i>City, County, State, Nation</li>
-                <li class="list-group-item"><i class="fa fa-envelope"></i><a href="#">Email@EmailAddress.com</a></li>
-                <li class="list-group-item"><i class="fa fa-link"></i><a href="#">http://yousite/</a></li>
-                <li class="list-group-item"><i class="fa fa-clock-o"></i>Joined At 03.02, 2014</li>
+                <li class="list-group-item"><i class="fa fa-thumb-tack"></i>{{.Owner.Location}}</li>
+                <li class="list-group-item"><i class="fa fa-envelope"></i><a href="#">{{.Owner.Email}}</a></li>
+                <li class="list-group-item"><i class="fa fa-link"></i><a href="#">{{.Owner.Website}}</a></li>
+                <li class="list-group-item"><i class="fa fa-clock-o"></i>{{.Owner.Created}}</li>
             </ul>
         </div>
     </div>
diff --git a/web.go b/web.go
index 475b662b39..856d0f106d 100644
--- a/web.go
+++ b/web.go
@@ -63,7 +63,7 @@ func runWeb(*cli.Context) {
 	m.Any("/user/delete", auth.SignInRequire(true), user.Delete)
 	m.Get("/user/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds)
 
-	m.Any("/user/setting", auth.SignInRequire(true), user.Setting)
+	m.Any("/user/setting", auth.SignInRequire(true), binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting)
 	m.Any("/user/setting/ssh", auth.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys)
 
 	m.Get("/user/:username", auth.SignInRequire(false), user.Profile)