Bug fix on organization

This commit is contained in:
Unknwon 2014-08-27 16:39:36 +08:00
parent 50de06056b
commit 9baf2b38d0

AI 샘플 코드 생성 중입니다

Loading...
12 changed files with 164 additions and 35 deletions

View File

@ -225,10 +225,6 @@ func runWeb(*cli.Context) {
r.Get("/create", org.Create) r.Get("/create", org.Create)
r.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost) r.Post("/create", bindIgnErr(auth.CreateOrgForm{}), org.CreatePost)
m.Group("/:org", func(r *macaron.Router) {
r.Get("", org.Home)
}, middleware.OrgAssignment(true))
m.Group("/:org", func(r *macaron.Router) { m.Group("/:org", func(r *macaron.Router) {
r.Get("/dashboard", user.Dashboard) r.Get("/dashboard", user.Dashboard)
r.Get("/members", org.Members) r.Get("/members", org.Members)
@ -257,6 +253,9 @@ func runWeb(*cli.Context) {
r.Route("/invitations/new", "GET,POST", org.Invitation) r.Route("/invitations/new", "GET,POST", org.Invitation)
}, middleware.OrgAssignment(true, true, true)) }, middleware.OrgAssignment(true, true, true))
}, reqSignIn) }, reqSignIn)
m.Group("/org", func(r *macaron.Router) {
r.Get("/:org", org.Home)
}, middleware.OrgAssignment(true))
// Repository routers. // Repository routers.
m.Group("/repo", func(r *macaron.Router) { m.Group("/repo", func(r *macaron.Router) {

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.4.7.0826 Alpha" const APP_VER = "0.4.7.0827 Alpha"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -337,6 +337,9 @@ func RemoveOrgUser(orgId, uid int64) error {
if _, err = sess.Delete(access); err != nil { if _, err = sess.Delete(access); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} else if err = WatchRepo(u.Id, repo.Id, false); err != nil {
sess.Rollback()
return err
} }
} }
@ -513,6 +516,10 @@ func (t *Team) AddRepository(repo *Repository) (err error) {
return err return err
} }
} }
if err = WatchRepo(u.Id, repo.Id, true); err != nil {
sess.Rollback()
return err
}
} }
return sess.Commit() return sess.Commit()
} }
@ -564,6 +571,9 @@ func (t *Team) RemoveRepository(repoId int64) error {
if _, err = sess.Delete(access); err != nil { if _, err = sess.Delete(access); err != nil {
sess.Rollback() sess.Rollback()
return fmt.Errorf("fail to delete access: %v", err) return fmt.Errorf("fail to delete access: %v", err)
} else if err = WatchRepo(u.Id, repo.Id, false); err != nil {
sess.Rollback()
return err
} }
} else if auth < t.Authorize { } else if auth < t.Authorize {
if err = addAccessWithAuthorize(sess, access, AuthorizeToAccessType(auth)); err != nil { if err = addAccessWithAuthorize(sess, access, AuthorizeToAccessType(auth)); err != nil {
@ -1029,6 +1039,9 @@ func removeTeamMemberWithSess(orgId, teamId, uid int64, sess *xorm.Session) erro
if _, err = sess.Delete(access); err != nil { if _, err = sess.Delete(access); err != nil {
sess.Rollback() sess.Rollback()
return fmt.Errorf("fail to delete access: %v", err) return fmt.Errorf("fail to delete access: %v", err)
} else if err = WatchRepo(u.Id, repo.Id, false); err != nil {
sess.Rollback()
return err
} }
} else if auth < t.Authorize { } else if auth < t.Authorize {
// Downgrade authorize level. // Downgrade authorize level.

View File

@ -288,6 +288,16 @@ func MigrateRepository(u *User, name, desc string, private, mirror bool, url str
repoPath := RepoPath(u.Name, name) repoPath := RepoPath(u.Name, name)
if u.IsOrganization() {
t, err := u.GetOwnerTeam()
if err != nil {
return nil, err
}
repo.NumWatches = t.NumMembers
} else {
repo.NumWatches = 1
}
repo.IsBare = false repo.IsBare = false
if mirror { if mirror {
if err = MirrorRepository(repo.Id, u.Name, repo.Name, repoPath, url); err != nil { if err = MirrorRepository(repo.Id, u.Name, repo.Name, repoPath, url); err != nil {
@ -559,20 +569,25 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror
} }
if u.IsOrganization() { if u.IsOrganization() {
ous, err := GetOrgUsersByOrgId(u.Id) t, err := u.GetOwnerTeam()
if err != nil { if err != nil {
log.Error(4, "GetOrgUsersByOrgId: %v", err) log.Error(4, "GetOwnerTeam: %v", err)
} else { } else {
for _, ou := range ous { if err = t.GetMembers(); err != nil {
if err = WatchRepo(ou.Uid, repo.Id, true); err != nil { log.Error(4, "GetMembers: %v", err)
log.Error(4, "WatchRepo: %v", err) } else {
} for _, u := range t.Members {
}
}
}
if err = WatchRepo(u.Id, repo.Id, true); err != nil { if err = WatchRepo(u.Id, repo.Id, true); err != nil {
log.Error(4, "WatchRepo2: %v", err) log.Error(4, "WatchRepo2: %v", err)
} }
}
}
}
} else {
if err = WatchRepo(u.Id, repo.Id, true); err != nil {
log.Error(4, "WatchRepo3: %v", err)
}
}
if err = NewRepoAction(u, repo); err != nil { if err = NewRepoAction(u, repo); err != nil {
log.Error(4, "NewRepoAction: %v", err) log.Error(4, "NewRepoAction: %v", err)
@ -669,18 +684,61 @@ func TransferOwnership(u *User, newOwner string, repo *Repository) (err error) {
} }
// Update user repository number. // Update user repository number.
rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", newUser.Id); err != nil {
if _, err = sess.Exec(rawSql, newUser.Id); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} }
rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?", u.Id); err != nil {
if _, err = sess.Exec(rawSql, u.Id); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} }
// New owner is organization.
if newUser.IsOrganization() {
mode := WRITABLE
if repo.IsMirror {
mode = READABLE
}
access := &Access{
RepoName: path.Join(newUser.LowerName, repo.LowerName),
Mode: mode,
}
// Give access to all members in owner team.
t, err := newUser.GetOwnerTeam()
if err != nil {
sess.Rollback()
return err
}
if err = t.GetMembers(); err != nil {
sess.Rollback()
return err
}
for _, u := range t.Members {
access.Id = 0
access.UserName = u.LowerName
if _, err = sess.Insert(access); err != nil {
sess.Rollback()
return err
}
}
if _, err = sess.Exec(
"UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil {
sess.Rollback()
return err
}
// Update owner team info and count.
t.RepoIds += "$" + com.ToStr(repo.Id) + "|"
t.NumRepos++
if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil {
sess.Rollback()
return err
}
}
// Change repository directory name. // Change repository directory name.
if err = os.Rename(RepoPath(u.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil { if err = os.Rename(RepoPath(u.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil {
sess.Rollback() sess.Rollback()
@ -692,11 +750,14 @@ func TransferOwnership(u *User, newOwner string, repo *Repository) (err error) {
} }
// Add watch of new owner to repository. // Add watch of new owner to repository.
if !IsWatching(newUser.Id, repo.Id) { if !newUser.IsOrganization() {
if err = WatchRepo(newUser.Id, repo.Id, true); err != nil { if err = WatchRepo(newUser.Id, repo.Id, true); err != nil {
return err log.Error(4, "WatchRepo", err)
} }
} }
if err = WatchRepo(u.Id, repo.Id, false); err != nil {
log.Error(4, "WatchRepo2", err)
}
if err = TransferRepoAction(u, newUser, repo); err != nil { if err = TransferRepoAction(u, newUser, repo); err != nil {
return err return err
@ -753,8 +814,8 @@ func UpdateRepository(repo *Repository) error {
} }
// DeleteRepository deletes a repository for a user or orgnaztion. // DeleteRepository deletes a repository for a user or orgnaztion.
func DeleteRepository(userId, repoId int64, userName string) error { func DeleteRepository(uid, repoId int64, userName string) error {
repo := &Repository{Id: repoId, OwnerId: userId} repo := &Repository{Id: repoId, OwnerId: uid}
has, err := x.Get(repo) has, err := x.Get(repo)
if err != nil { if err != nil {
return err return err
@ -762,6 +823,17 @@ func DeleteRepository(userId, repoId int64, userName string) error {
return ErrRepoNotExist return ErrRepoNotExist
} }
// In case is a organization.
org, err := GetUserById(uid)
if err != nil {
return err
}
if org.IsOrganization() {
if err = org.GetTeams(); err != nil {
return err
}
}
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() defer sess.Close()
if err = sess.Begin(); err != nil { if err = sess.Begin(); err != nil {
@ -772,10 +844,27 @@ func DeleteRepository(userId, repoId int64, userName string) error {
sess.Rollback() sess.Rollback()
return err return err
} }
// Delete all access.
if _, err := sess.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil { if _, err := sess.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} }
if org.IsOrganization() {
idStr := "$" + com.ToStr(repoId) + "|"
for _, t := range org.Teams {
if !strings.Contains(t.RepoIds, idStr) {
continue
}
t.NumRepos--
t.RepoIds = strings.Replace(t.RepoIds, idStr, "", 1)
if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil {
sess.Rollback()
return err
}
}
}
if _, err := sess.Delete(&Action{RepoId: repo.Id}); err != nil { if _, err := sess.Delete(&Action{RepoId: repo.Id}); err != nil {
sess.Rollback() sess.Rollback()
return err return err
@ -819,8 +908,7 @@ func DeleteRepository(userId, repoId int64, userName string) error {
return err return err
} }
rawSql := "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?", uid); err != nil {
if _, err = sess.Exec(rawSql, userId); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} }

View File

@ -210,13 +210,19 @@ func (w *FileLogWriter) DoRotate() error {
func (w *FileLogWriter) deleteOldLog() { func (w *FileLogWriter) deleteOldLog() {
dir := filepath.Dir(w.Filename) dir := filepath.Dir(w.Filename)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
defer func() {
if r := recover(); r != nil {
returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r)
}
}()
if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) { if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) {
if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) { if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) {
os.Remove(path) os.Remove(path)
} }
} }
return nil return returnErr
}) })
} }

View File

@ -225,7 +225,9 @@ func (l *Logger) StartLogger() {
select { select {
case bm := <-l.msg: case bm := <-l.msg:
for _, l := range l.outputs { for _, l := range l.outputs {
l.WriteMsg(bm.msg, bm.skip, bm.level) if err := l.WriteMsg(bm.msg, bm.skip, bm.level); err != nil {
fmt.Println("ERROR, unable to WriteMsg:", err)
}
} }
case <-l.quit: case <-l.quit:
return return
@ -247,7 +249,9 @@ func (l *Logger) Close() {
if len(l.msg) > 0 { if len(l.msg) > 0 {
bm := <-l.msg bm := <-l.msg
for _, l := range l.outputs { for _, l := range l.outputs {
l.WriteMsg(bm.msg, bm.skip, bm.level) if err := l.WriteMsg(bm.msg, bm.skip, bm.level); err != nil {
fmt.Println("ERROR, unable to WriteMsg:", err)
}
} }
} else { } else {
break break

View File

@ -56,6 +56,9 @@ func OrgAssignment(redirect bool, args ...bool) macaron.Handler {
ctx.Org.IsMember = true ctx.Org.IsMember = true
} }
} }
} else {
// Fake data.
ctx.Data["SignedUser"] = &models.User{}
} }
if (requireMember && !ctx.Org.IsMember) || if (requireMember && !ctx.Org.IsMember) ||
(requireOwner && !ctx.Org.IsOwner) { (requireOwner && !ctx.Org.IsOwner) {

View File

@ -218,6 +218,8 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
setting.Cfg.SetValue("", "RUN_MODE", "prod") setting.Cfg.SetValue("", "RUN_MODE", "prod")
setting.Cfg.SetValue("log", "MODE", "file")
setting.Cfg.SetValue("security", "INSTALL_LOCK", "true") setting.Cfg.SetValue("security", "INSTALL_LOCK", "true")
os.MkdirAll("custom/conf", os.ModePerm) os.MkdirAll("custom/conf", os.ModePerm)

View File

@ -142,8 +142,9 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
ctx.Data["Title"] = ctx.Tr("new_migrate") ctx.Data["Title"] = ctx.Tr("new_migrate")
ctxUser := ctx.User ctxUser := ctx.User
if orgId := com.StrTo(ctx.Query("org")).MustInt64(); orgId > 0 { // Not equal means current user is an organization.
org, err := models.GetUserById(orgId) if form.Uid != ctx.User.Id {
org, err := models.GetUserById(form.Uid)
if err != nil && err != models.ErrUserNotExist { if err != nil && err != models.ErrUserNotExist {
ctx.Handle(500, "GetUserById", err) ctx.Handle(500, "GetUserById", err)
return return

View File

@ -119,10 +119,23 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
if ctx.Repo.Repository.Name != form.RepoName { if ctx.Repo.Repository.Name != form.RepoName {
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil) ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
return return
} else if !ctx.Repo.Owner.ValidtePassword(ctx.Query("password")) { }
if ctx.Repo.Owner.IsOrganization() {
if !ctx.Repo.Owner.IsOrgOwner(ctx.User.Id) {
ctx.Error(404)
return
}
if !ctx.User.ValidtePassword(ctx.Query("password")) {
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil) ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil)
return return
} }
} else {
if !ctx.Repo.Owner.ValidtePassword(ctx.Query("password")) {
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil)
return
}
}
if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil { if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil {
ctx.Handle(500, "DeleteRepository", err) ctx.Handle(500, "DeleteRepository", err)

View File

@ -1 +1 @@
0.4.7.0826 Alpha 0.4.7.0827 Alpha

View File

@ -10,8 +10,8 @@
<h2>{{.i18n.Tr "app_desc"}}</h2> <h2>{{.i18n.Tr "app_desc"}}</h2>
<form id="promo-form" action="/user/login" method="post"> <form id="promo-form" action="/user/login" method="post">
{{.CsrfTokenHtml}} {{.CsrfTokenHtml}}
<input class="ipt ipt-large" id="username" name="uname" type="text" placeholder="{{.i18n.Tr " home.uname_holder "}}"/> <input class="ipt ipt-large" id="username" name="uname" type="text" placeholder="{{.i18n.Tr "home.uname_holder"}}"/>
<input class="ipt ipt-large" name="password" type="password" placeholder="{{.i18n.Tr " home.password_holder "}}"/> <input class="ipt ipt-large" name="password" type="password" placeholder="{{.i18n.Tr "home.password_holder"}}"/>
<input name="from" type="hidden" value="home"> <input name="from" type="hidden" value="home">
<button class="btn btn-black btn-large">{{.i18n.Tr "sign_in"}}</button> <button class="btn btn-black btn-large">{{.i18n.Tr "sign_in"}}</button>
<button class="btn btn-green btn-large" id="register-button">{{.i18n.Tr "register"}}</button> <button class="btn btn-green btn-large" id="register-button">{{.i18n.Tr "register"}}</button>