diff --git a/cmd/web.go b/cmd/web.go index cc9fa385..764e2c1b 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -225,10 +225,6 @@ func runWeb(*cli.Context) { r.Get("/create", org.Create) 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) { r.Get("/dashboard", user.Dashboard) r.Get("/members", org.Members) @@ -257,6 +253,9 @@ func runWeb(*cli.Context) { r.Route("/invitations/new", "GET,POST", org.Invitation) }, middleware.OrgAssignment(true, true, true)) }, reqSignIn) + m.Group("/org", func(r *macaron.Router) { + r.Get("/:org", org.Home) + }, middleware.OrgAssignment(true)) // Repository routers. m.Group("/repo", func(r *macaron.Router) { diff --git a/gogs.go b/gogs.go index 4adac90b..724a6835 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.4.7.0826 Alpha" +const APP_VER = "0.4.7.0827 Alpha" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/org.go b/models/org.go index 5d73cf3f..1c74c290 100644 --- a/models/org.go +++ b/models/org.go @@ -337,6 +337,9 @@ func RemoveOrgUser(orgId, uid int64) error { if _, err = sess.Delete(access); err != nil { sess.Rollback() 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 } } + if err = WatchRepo(u.Id, repo.Id, true); err != nil { + sess.Rollback() + return err + } } return sess.Commit() } @@ -564,6 +571,9 @@ func (t *Team) RemoveRepository(repoId int64) error { if _, err = sess.Delete(access); err != nil { sess.Rollback() 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 { 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 { sess.Rollback() 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 { // Downgrade authorize level. diff --git a/models/repo.go b/models/repo.go index 62bac9f1..47036966 100644 --- a/models/repo.go +++ b/models/repo.go @@ -288,6 +288,16 @@ func MigrateRepository(u *User, name, desc string, private, mirror bool, url str 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 if mirror { if err = MirrorRepository(repo.Id, u.Name, repo.Name, repoPath, url); err != nil { @@ -559,19 +569,24 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror } if u.IsOrganization() { - ous, err := GetOrgUsersByOrgId(u.Id) + t, err := u.GetOwnerTeam() if err != nil { - log.Error(4, "GetOrgUsersByOrgId: %v", err) + log.Error(4, "GetOwnerTeam: %v", err) } else { - for _, ou := range ous { - if err = WatchRepo(ou.Uid, repo.Id, true); err != nil { - log.Error(4, "WatchRepo: %v", err) + if err = t.GetMembers(); err != nil { + log.Error(4, "GetMembers: %v", err) + } else { + for _, u := range t.Members { + if err = WatchRepo(u.Id, repo.Id, true); err != nil { + log.Error(4, "WatchRepo2: %v", err) + } } } } - } - if err = WatchRepo(u.Id, repo.Id, true); err != nil { - 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 { @@ -669,18 +684,61 @@ func TransferOwnership(u *User, newOwner string, repo *Repository) (err error) { } // Update user repository number. - rawSql := "UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, newUser.Id); err != nil { + if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", newUser.Id); err != nil { sess.Rollback() return err } - rawSql = "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, u.Id); err != nil { + if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?", u.Id); err != nil { sess.Rollback() 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. if err = os.Rename(RepoPath(u.Name, repo.Name), RepoPath(newUser.Name, repo.Name)); err != nil { sess.Rollback() @@ -692,11 +750,14 @@ func TransferOwnership(u *User, newOwner string, repo *Repository) (err error) { } // 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 { - 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 { return err @@ -753,8 +814,8 @@ func UpdateRepository(repo *Repository) error { } // DeleteRepository deletes a repository for a user or orgnaztion. -func DeleteRepository(userId, repoId int64, userName string) error { - repo := &Repository{Id: repoId, OwnerId: userId} +func DeleteRepository(uid, repoId int64, userName string) error { + repo := &Repository{Id: repoId, OwnerId: uid} has, err := x.Get(repo) if err != nil { return err @@ -762,6 +823,17 @@ func DeleteRepository(userId, repoId int64, userName string) error { 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() defer sess.Close() if err = sess.Begin(); err != nil { @@ -772,10 +844,27 @@ func DeleteRepository(userId, repoId int64, userName string) error { sess.Rollback() return err } + + // Delete all access. if _, err := sess.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil { sess.Rollback() 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 { sess.Rollback() return err @@ -819,8 +908,7 @@ func DeleteRepository(userId, repoId int64, userName string) error { return err } - rawSql := "UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?" - if _, err = sess.Exec(rawSql, userId); err != nil { + if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos - 1 WHERE id = ?", uid); err != nil { sess.Rollback() return err } diff --git a/modules/log/file.go b/modules/log/file.go index 52a2f68c..e9402815 100644 --- a/modules/log/file.go +++ b/modules/log/file.go @@ -210,13 +210,19 @@ func (w *FileLogWriter) DoRotate() error { func (w *FileLogWriter) deleteOldLog() { 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 strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) { os.Remove(path) } } - return nil + return returnErr }) } diff --git a/modules/log/log.go b/modules/log/log.go index 00f777e7..8f4de1e1 100644 --- a/modules/log/log.go +++ b/modules/log/log.go @@ -225,7 +225,9 @@ func (l *Logger) StartLogger() { select { case bm := <-l.msg: 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: return @@ -247,7 +249,9 @@ func (l *Logger) Close() { if len(l.msg) > 0 { bm := <-l.msg 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 { break diff --git a/modules/middleware/org.go b/modules/middleware/org.go index ee4460b2..7bb24ab7 100644 --- a/modules/middleware/org.go +++ b/modules/middleware/org.go @@ -56,6 +56,9 @@ func OrgAssignment(redirect bool, args ...bool) macaron.Handler { ctx.Org.IsMember = true } } + } else { + // Fake data. + ctx.Data["SignedUser"] = &models.User{} } if (requireMember && !ctx.Org.IsMember) || (requireOwner && !ctx.Org.IsOwner) { diff --git a/routers/install.go b/routers/install.go index c63ac690..3ac35f35 100644 --- a/routers/install.go +++ b/routers/install.go @@ -218,6 +218,8 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) { setting.Cfg.SetValue("", "RUN_MODE", "prod") + setting.Cfg.SetValue("log", "MODE", "file") + setting.Cfg.SetValue("security", "INSTALL_LOCK", "true") os.MkdirAll("custom/conf", os.ModePerm) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 7356f503..bbc7cf85 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -142,8 +142,9 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_migrate") ctxUser := ctx.User - if orgId := com.StrTo(ctx.Query("org")).MustInt64(); orgId > 0 { - org, err := models.GetUserById(orgId) + // Not equal means current user is an organization. + if form.Uid != ctx.User.Id { + org, err := models.GetUserById(form.Uid) if err != nil && err != models.ErrUserNotExist { ctx.Handle(500, "GetUserById", err) return diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 866c0cd9..24c1b13a 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -119,9 +119,22 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) { if ctx.Repo.Repository.Name != form.RepoName { ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil) return - } else if !ctx.Repo.Owner.ValidtePassword(ctx.Query("password")) { - ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil) - return + } + + 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) + 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 { diff --git a/templates/.VERSION b/templates/.VERSION index 35fc8ef1..f7f2473b 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.4.7.0826 Alpha \ No newline at end of file +0.4.7.0827 Alpha \ No newline at end of file diff --git a/templates/home.tmpl b/templates/home.tmpl index a0dd408f..2825e14a 100644 --- a/templates/home.tmpl +++ b/templates/home.tmpl @@ -10,8 +10,8 @@