2016-11-04 11:25:53 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package sql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/abcum/fibre"
|
|
|
|
"github.com/abcum/surreal/cnf"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Root access
|
|
|
|
AuthKV int = iota
|
|
|
|
// Namespace access
|
|
|
|
AuthNS
|
|
|
|
// Database access
|
|
|
|
AuthDB
|
|
|
|
// Scoped user access
|
2016-11-14 13:26:07 +00:00
|
|
|
AuthSC
|
2016-11-04 11:25:53 +00:00
|
|
|
// No access
|
|
|
|
AuthNO
|
|
|
|
)
|
|
|
|
|
|
|
|
// options represents context runtime config.
|
|
|
|
type options struct {
|
2016-11-21 18:48:25 +00:00
|
|
|
auth *cnf.Auth
|
2016-11-04 11:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newOptions(c *fibre.Context) *options {
|
|
|
|
return &options{
|
2016-11-21 18:48:25 +00:00
|
|
|
auth: c.Get("auth").(*cnf.Auth),
|
2016-11-04 11:25:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) get(kind int) (kv, ns, db string, err error) {
|
|
|
|
|
|
|
|
kv = cnf.Settings.DB.Base
|
2016-11-21 18:48:25 +00:00
|
|
|
ns = o.auth.Selected.NS
|
|
|
|
db = o.auth.Selected.DB
|
2016-11-04 11:25:53 +00:00
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
if kind < o.auth.Kind {
|
2016-11-04 11:25:53 +00:00
|
|
|
err = &QueryError{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ns == "" || db == "" {
|
|
|
|
err = &BlankError{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) ns(ns string) (err error) {
|
|
|
|
|
|
|
|
// Check to see that the current user has
|
2017-02-09 20:44:08 +00:00
|
|
|
// the necessary authentication privileges
|
2016-11-04 11:25:53 +00:00
|
|
|
// to be able to specify this namespace.
|
|
|
|
|
2016-11-14 10:20:45 +00:00
|
|
|
if o.auth.Possible.NS != "*" && o.auth.Possible.NS != ns {
|
|
|
|
return &NSError{NS: ns}
|
2016-11-04 11:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Specify the NS on the context session, so
|
|
|
|
// that it is remembered across requests on
|
|
|
|
// any persistent connections.
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
o.auth.Selected.NS = ns
|
2016-11-04 11:25:53 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *options) db(db string) (err error) {
|
|
|
|
|
|
|
|
// Check to see that the current user has
|
2017-02-09 20:44:08 +00:00
|
|
|
// the necessary authentication privileges
|
2016-11-04 11:25:53 +00:00
|
|
|
// to be able to specify this namespace.
|
|
|
|
|
2016-11-14 10:20:45 +00:00
|
|
|
if o.auth.Possible.DB != "*" && o.auth.Possible.DB != db {
|
|
|
|
return &DBError{DB: db}
|
2016-11-04 11:25:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Specify the DB on the context session, so
|
|
|
|
// that it is remembered across requests on
|
|
|
|
// any persistent connections.
|
|
|
|
|
2016-11-21 18:48:25 +00:00
|
|
|
o.auth.Selected.DB = db
|
2016-11-04 11:25:53 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|