// UserIsOnlyWriter returns true if and only if username is the only writer in
// a TLF represented by canon. In any error case, false is returned. This
// function only naively looks at the TLF name, so it should only be used on
// non-team TLFs.
198150
func UserIsOnlyWriter(username kbname.NormalizedUsername, canon CanonicalName) bool {
	tlfname := string(canon)
	if len(username) == 0 {
		return false
	}
	ws, _, _, err := SplitName(tlfname)
	if err != nil {
		return false
	}
	return len(ws) == 1 && ws[0] == string(username)
}
785583
func isSysFSWritable() (bool, error) {
	const permWritable = "rw"
	const sysfsDevice = "sysfs"
	m := mount.New("" /* default mount path */)
	mountPoints, err := m.List()
	if err != nil {
		klog.Errorf("failed to list mount points: %v", err)
		return false, err
	}

	for _, mountPoint := range mountPoints {
		if mountPoint.Type != sysfsDevice {
			continue
		}
		// Check whether sysfs is 'rw'
		if len(mountPoint.Opts) > 0 && mountPoint.Opts[0] == permWritable {
			return true, nil
		}
		klog.Errorf("sysfs is not writable: %+v (mount options are %v)",
			mountPoint, mountPoint.Opts)
		return false, errReadOnlySysFS
	}

	return false, errors.New("no sysfs mounted")
}
189265
func (md *RootMetadataV2) IsWriter(
	_ context.Context, user keybase1.UID, deviceKey kbfscrypto.CryptPublicKey,
	_ kbfscrypto.VerifyingKey, _ TeamMembershipChecker, _ ExtraMetadata,
	_ keybase1.OfflineAvailability) (bool, error) {
	if md.ID.Type() != tlf.Private {
		for _, w := range md.Writers {
			if w == user.AsUserOrTeam() {
				return true, nil
			}
		}
		return false, nil
	}
	return md.WKeys.IsWriter(user, deviceKey), nil
}
1336464
func (state *UserState) FindUserByConfirmationCode(confirmationcode string) (string, error) {
	unconfirmedUsernames, err := state.AllUnconfirmedUsernames()
	if err != nil {
		return "", ErrAllUsersConfirmedAlready
	}

	// Find the username by looking up the confirmationcode on unconfirmed users
	username := ""
	for _, aUsername := range unconfirmedUsernames {
		aConfirmationCode, err := state.ConfirmationCode(aUsername)
		if err != nil {
			// If the confirmation code can not be found, just skip this one
			continue
		}
		if confirmationcode == aConfirmationCode {
			// Found the right user
			username = aUsername
			break
		}
	}

	// Check that the user is there
	if username == "" {
		return username, ErrConfirmationCodeExpired
	}
	if !state.HasUser(username) {
		return username, ErrMissingUserAtConfirm
	}

	return username, nil
}
179193
func (m *Manager) AllByUser(username string) ([]Organization, error) {
	var organizations []Organization
	//TODO: handle this a bit smarter, select only the ones where the user is owner first, and take select only the org name
	//do the same for the orgs where the username is member but not owners
	//No need to pull in 1000's of records for this

	condition := []interface{}{
		bson.M{"members": username},
		bson.M{"owners": username},
	}

	err := m.collection.Find(bson.M{"$or": condition}).All(&organizations)

	return organizations, err
}
110881
func (md *RootMetadataV3) SignWriterMetadataInternally(
	ctx context.Context, codec kbfscodec.Codec,
	signer kbfscrypto.Signer) error {
	// Nothing to do.
	//
	// TODO: Set a flag, and a way to check it so that we can
	// verify that this is called before sending to the server.
	return nil
}
1191990
func AuthUsersByUsername(db XODB, username string) ([]*AuthUser, error) {
	var err error

	// sql query
	const sqlstr = `SELECT ` +
		`id, password, last_login, is_superuser, username, first_name, last_name, email, is_staff, is_active, date_joined ` +
		`FROM public.auth_user ` +
		`WHERE username = $1`

	// run query
	XOLog(sqlstr, username)
	q, err := db.Query(sqlstr, username)
	if err != nil {
		return nil, err
	}
	defer q.Close()

	// load results
	res := []*AuthUser{}
	for q.Next() {
		au := AuthUser{
			_exists: true,
		}

		// scan
		err = q.Scan(&au.ID, &au.Password, &au.LastLogin, &au.IsSuperuser, &au.Username, &au.FirstName, &au.LastName, &au.Email, &au.IsStaff, &au.IsActive, &au.DateJoined)
		if err != nil {
			return nil, err
		}

		res = append(res, &au)
	}

	return res, nil
}
538439
func IsProjectWriter(c context.Context, pcfg *svcconfig.ProjectConfig) error {
	return checkMember(c, pcfg.WriterAuthGroups...)
}
866091
func (wkg TLFWriterKeyGenerationsV2) IsWriter(user keybase1.UID, deviceKey kbfscrypto.CryptPublicKey) bool {
	keyGen := wkg.LatestKeyGeneration()
	if keyGen < 1 {
		return false
	}
	return wkg[keyGen-1].IsWriter(user, deviceKey)
}
1258925
func (u User) PartialCopy() *User {
	ret := &User{
		Contextified: NewContextified(u.G()),
		id:           u.id,
		name:         u.name,
		leaf:         u.leaf,
		dirty:        false,
	}
	if ckf := u.GetComputedKeyFamily(); ckf != nil {
		ret.ckfShallowCopy = ckf.ShallowCopy()
		ret.keyFamily = ckf.kf
	} else if u.keyFamily != nil {
		ret.keyFamily = u.keyFamily.ShallowCopy()
	}
	return ret
}
1142354
func NewTSMWriter(w io.Writer) (TSMWriter, error) {
	index := NewIndexWriter()
	return &tsmWriter{
		wrapped: w,
		w:       bufio.NewWriterSize(w, 1024*1024),
		index:   index,
		stats:   NewMeasurementStats(),
	}, nil
}
1219510
func IsTerminalWriter(w io.Writer) bool {
	file, ok := w.(*os.File)
	return ok && term.IsTerminal(file.Fd())
}
1061569
func (state *UserState) FindUserByConfirmationCode(confirmationCode string) (string, error) {
	unconfirmedUsernames, err := state.AllUnconfirmedUsernames()
	if err != nil {
		return "", errors.New("all existing users are already confirmed")
	}

	// Find the username by looking up the confirmationCode on unconfirmed users
	username := ""
	for _, aUsername := range unconfirmedUsernames {
		aConfirmationCode, err := state.ConfirmationCode(aUsername)
		if err != nil {
			// If the confirmation code can not be found, just skip this one
			continue
		}
		if confirmationCode == aConfirmationCode {
			// Found the right user
			username = aUsername
			break
		}
	}

	// Check that the user is there
	if username == "" {
		return username, errors.New("the confirmation code is no longer valid")
	}
	hasUser := state.HasUser(username)
	if !hasUser {
		return username, errors.New("the user that is to be confirmed no longer exists")
	}

	return username, nil
}
554001
func MultiLevelWriter(writers ...io.Writer) LevelWriter {
	lwriters := make([]LevelWriter, 0, len(writers))
	for _, w := range writers {
		if lw, ok := w.(LevelWriter); ok {
			lwriters = append(lwriters, lw)
		} else {
			lwriters = append(lwriters, levelWriterAdapter{w})
		}
	}
	return multiLevelWriter{lwriters}
}
801231
func UserAlreadyExistsError(n string) *influxdb.Error {
	return &influxdb.Error{
		Code: influxdb.EConflict,
		Msg:  fmt.Sprintf("user with name %s already exists", n),
	}
}
784463
func (sl *Slack) FindUserByName(name string) (*User, error) {
	return sl.FindUser(func(user *User) bool {
		return user.Name == name
	})
}
917848
func UsernameIsValid(username string) bool {
	if username == "" {
		return false
	}

	extracted := extract.ExtractMentionedScreenNames(username)
	return len(extracted) == 1 && extracted[0].Text == username
}