Exemple #1
0
// UpdateAccess updates access information with session for rolling back.
func UpdateAccessWithSession(sess *xorm.Session, access *Access) error {
	if _, err := sess.Id(access.Id).Update(access); err != nil {
		sess.Rollback()
		return err
	}
	return nil
}
Exemple #2
0
func (c *XormController) doTransaction(session *xorm.Session, handler SessionHandlerFunc) error {
	err := session.Begin()
	if err != nil {
		return err
	}

	if err = handler(session); err == nil {
		session.Commit()
	} else {
		session.Rollback()
	}
	return err
}
Exemple #3
0
func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
	mg.Logger.Info("Executing migration", "id", m.Id())

	condition := m.GetCondition()
	if condition != nil {
		sql, args := condition.Sql(mg.dialect)
		results, err := sess.Query(sql, args...)
		if err != nil || len(results) == 0 {
			mg.Logger.Info("Skipping migration condition not fulfilled", "id", m.Id())
			return sess.Rollback()
		}
	}

	_, err := sess.Exec(m.Sql(mg.dialect))
	if err != nil {
		mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err)
		return err
	}

	return nil
}
Exemple #4
0
func sessionRelease(sess *xorm.Session) {
	if !sess.IsCommitedOrRollbacked {
		sess.Rollback()
	}
	sess.Close()
}
Exemple #5
0
func removeTeamMemberWithSess(orgId, teamId, uid int64, sess *xorm.Session) error {
	if !IsTeamMember(orgId, teamId, uid) {
		return nil
	}

	// Get team and its repositories.
	t, err := GetTeamById(teamId)
	if err != nil {
		return err
	}

	// Check if the user to delete is the last member in owner team.
	if t.IsOwnerTeam() && t.NumMembers == 1 {
		return ErrLastOrgOwner
	}

	t.NumMembers--

	if err = t.GetRepositories(); err != nil {
		return err
	}

	// Get organization.
	org, err := GetUserById(orgId)
	if err != nil {
		return err
	}

	// Get user.
	u, err := GetUserById(uid)
	if err != nil {
		return err
	}

	tu := &TeamUser{
		Uid:    uid,
		OrgId:  orgId,
		TeamId: teamId,
	}

	if _, err := sess.Delete(tu); err != nil {
		sess.Rollback()
		return err
	} else if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil {
		sess.Rollback()
		return err
	}

	// Delete access to team repositories.
	for _, repo := range t.Repos {
		auth, err := GetHighestAuthorize(t.OrgId, u.Id, repo.Id, teamId)
		if err != nil {
			sess.Rollback()
			return err
		}

		access := &Access{
			UserName: u.LowerName,
			RepoName: path.Join(org.LowerName, repo.LowerName),
		}
		// Delete access if this is the last team user belongs to.
		if auth == 0 {
			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.
			if err = addAccessWithAuthorize(sess, access, AuthorizeToAccessType(auth)); err != nil {
				sess.Rollback()
				return err
			}
		}
	}

	// This must exist.
	ou := new(OrgUser)
	_, err = sess.Where("uid=?", uid).And("org_id=?", org.Id).Get(ou)
	if err != nil {
		sess.Rollback()
		return err
	}
	ou.NumTeams--
	if t.IsOwnerTeam() {
		ou.IsOwner = false
	}
	if _, err = sess.Id(ou.Id).AllCols().Update(ou); err != nil {
		sess.Rollback()
		return err
	}
	return nil
}