ducky/devices

Paddy 2015-12-19 Parent:b6494e1a499e

16:a700ede02f91 Go to Latest

ducky/devices/vendor/code.secondbit.org/uuid.hg/uuid.go

Validate device creation. Update our uuid package to the latest, which is now based on the GitHub fork instead of the Google Code. Also, update our api package to its latest version, which now needs the pqarrays package as a dependency. We fleshed out the validateDeviceCreation. We now pass in the scopes we have (for broad access control) and the user ID (for fine-grained access control). This helper returns the first error it encounters, though it should probably return a slice so we can return multiple errors all at once. Before we even decode the request to create a Device, let's check if the user is even logged in. If we can't ascertain that or they're not, there's no point in even consuming the memory necessary to read the request, because we know we're not going to use it anyways. Finally actually validate the devices we're creating, and return an appropriate error for each error we can get. Also, the api.CheckScopes helper function now takes the scopes passed in as a string slice, and we have an api.GetScopes helper function to retrieve the scopes associated with the request. Let's not keep parsing that. We need two new scopes to control access for device creation; ScopeImport lets users import devices in and is pretty much admin access. ScopeCreateOtherUserDevices allows a user to create Devices that are owned by another user.

History
     1.1 --- a/vendor/code.secondbit.org/uuid.hg/uuid.go	Mon Dec 14 00:12:33 2015 -0800
     1.2 +++ b/vendor/code.secondbit.org/uuid.hg/uuid.go	Sat Dec 19 00:18:25 2015 -0800
     1.3 @@ -2,11 +2,9 @@
     1.4  
     1.5  import (
     1.6  	"database/sql/driver"
     1.7 -	"encoding/json"
     1.8 -	"encoding/xml"
     1.9  	"errors"
    1.10  
    1.11 -	"code.google.com/p/go-uuid/uuid"
    1.12 +	"github.com/pborman/uuid"
    1.13  )
    1.14  
    1.15  var InvalidIDError = errors.New("Invalid ID format.")
    1.16 @@ -45,11 +43,7 @@
    1.17  }
    1.18  
    1.19  func (id ID) MarshalJSON() ([]byte, error) {
    1.20 -	return json.Marshal(id.String())
    1.21 -}
    1.22 -
    1.23 -func (id ID) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    1.24 -	return e.EncodeElement(id.String(), start)
    1.25 +	return uuid.UUID(id).MarshalJSON()
    1.26  }
    1.27  
    1.28  func (id ID) Value() (driver.Value, error) {
    1.29 @@ -57,65 +51,11 @@
    1.30  }
    1.31  
    1.32  func (id *ID) Scan(src interface{}) error {
    1.33 -	if src == nil {
    1.34 -		id = nil
    1.35 -		return nil
    1.36 -	}
    1.37 -	switch src.(type) {
    1.38 -	case []byte:
    1.39 -		if len(src.([]byte)) == 0 {
    1.40 -			*id = (*id)[:0]
    1.41 -			return nil
    1.42 -		}
    1.43 -		newID, err := Parse(string(src.([]byte)))
    1.44 -		if err != nil {
    1.45 -			return err
    1.46 -		}
    1.47 -		*id = append((*id)[:0], newID...)
    1.48 -		return nil
    1.49 -	case string:
    1.50 -		if len(src.(string)) == 0 {
    1.51 -			*id = (*id)[:0]
    1.52 -			return nil
    1.53 -		}
    1.54 -		newID, err := Parse(src.(string))
    1.55 -		if err != nil {
    1.56 -			return err
    1.57 -		}
    1.58 -		*id = append((*id)[:0], newID...)
    1.59 -		return nil
    1.60 -	default:
    1.61 -		return InvalidIDError
    1.62 -	}
    1.63 -	return nil
    1.64 +	return (*uuid.UUID)(id).Scan(src)
    1.65  }
    1.66  
    1.67  func (id *ID) UnmarshalJSON(in []byte) error {
    1.68 -	var tmp string
    1.69 -	err := json.Unmarshal(in, &tmp)
    1.70 -	if err != nil {
    1.71 -		return err
    1.72 -	}
    1.73 -	newID, err := Parse(tmp)
    1.74 -	if err != nil {
    1.75 -		return err
    1.76 -	}
    1.77 -	*id = append((*id)[:0], newID...)
    1.78 -	return nil
    1.79 -}
    1.80 -
    1.81 -func (id *ID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    1.82 -	var tmp string
    1.83 -	err := d.DecodeElement(&tmp, &start)
    1.84 -	if err != nil {
    1.85 -		return err
    1.86 -	}
    1.87 -	newID, err := Parse(tmp)
    1.88 -	if err != nil {
    1.89 -		return err
    1.90 -	}
    1.91 -	*id = append((*id)[:0], newID...)
    1.92 -	return nil
    1.93 +	return (*uuid.UUID)(id).UnmarshalJSON(in)
    1.94  }
    1.95  
    1.96  func Parse(in string) (ID, error) {