示例#1
0
文件: table.go 项目: tradia/gotable
func (tbl *Table) Auth(req *PkgArgs, au Authorize) []byte {
	var in proto.PkgOneOp
	_, err := in.Decode(req.Pkg)
	if err != nil {
		in.CtrlFlag &^= 0xFF // Clear all ctrl flags
		in.SetErrCode(table.EcDecodeFail)
		return replyHandle(&in)
	}

	in.ErrCode = 0
	var authDB uint8

	var already bool
	if au.IsAuth(proto.AdminDbId) {
		authDB = proto.AdminDbId
		already = true
	} else if in.DbId != proto.AdminDbId && au.IsAuth(in.DbId) {
		authDB = in.DbId
		already = true
	}
	if already {
		return replyHandle(&in)
	}

	password := string(in.RowKey)

	tbl.mtx.Lock()
	// Admin password
	if tbl.authPwd == nil || tbl.authPwd[proto.AdminDbId] == password {
		authDB = proto.AdminDbId
	} else {
		// Selected DB password
		if len(password) > 0 && tbl.authPwd[in.DbId] == password {
			authDB = in.DbId
		} else {
			in.SetErrCode(table.EcAuthFailed)
		}
	}
	tbl.mtx.Unlock()

	// Success
	if in.ErrCode == 0 {
		in.DbId = authDB
		au.SetAuth(authDB)
	}

	return replyHandle(&in)
}