github.com/cs3org/reva/v2@v2.27.7/pkg/siteacc/data/site.go (about)

     1  // Copyright 2018-2020 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package data
    20  
    21  import (
    22  	"github.com/cs3org/reva/v2/pkg/siteacc/credentials"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // Site represents the global site-specific settings stored in the service.
    27  type Site struct {
    28  	ID string `json:"id"`
    29  
    30  	Config SiteConfiguration `json:"config"`
    31  }
    32  
    33  // SiteConfiguration stores the global configuration of a site.
    34  type SiteConfiguration struct {
    35  	TestClientCredentials credentials.Credentials `json:"testClientCredentials"`
    36  }
    37  
    38  // Sites holds an array of sites.
    39  type Sites = []*Site
    40  
    41  // Update copies the data of the given site to this site.
    42  func (site *Site) Update(other *Site, credsPassphrase string) error {
    43  	if other.Config.TestClientCredentials.IsValid() {
    44  		// If credentials were provided, use those as the new ones
    45  		if err := site.UpdateTestClientCredentials(other.Config.TestClientCredentials.ID, other.Config.TestClientCredentials.Secret, credsPassphrase); err != nil {
    46  			return err
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // UpdateTestClientCredentials assigns new test client credentials, encrypting the information first.
    54  func (site *Site) UpdateTestClientCredentials(id, secret string, passphrase string) error {
    55  	if err := site.Config.TestClientCredentials.Set(id, secret, passphrase); err != nil {
    56  		return errors.Wrap(err, "unable to update the test client credentials")
    57  	}
    58  	return nil
    59  }
    60  
    61  // Clone creates a copy of the site; if eraseCredentials is set to true, the (test user) credentials will be cleared in the cloned object.
    62  func (site *Site) Clone(eraseCredentials bool) *Site {
    63  	clone := *site
    64  
    65  	if eraseCredentials {
    66  		clone.Config.TestClientCredentials.Clear()
    67  	}
    68  
    69  	return &clone
    70  }
    71  
    72  // NewSite creates a new site.
    73  func NewSite(id string) (*Site, error) {
    74  	site := &Site{
    75  		ID: id,
    76  		Config: SiteConfiguration{
    77  			TestClientCredentials: credentials.Credentials{},
    78  		},
    79  	}
    80  	return site, nil
    81  }