github.com/cs3org/reva/v2@v2.27.7/pkg/siteacc/manager/gocdb/gocdb.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 gocdb
    20  
    21  import (
    22  	"github.com/cs3org/reva/v2/pkg/siteacc/config"
    23  	"github.com/cs3org/reva/v2/pkg/siteacc/data"
    24  	"github.com/pkg/errors"
    25  	"github.com/rs/zerolog"
    26  )
    27  
    28  // AccountsListener is the GOCDB accounts listener.
    29  type AccountsListener struct {
    30  	conf *config.Configuration
    31  	log  *zerolog.Logger
    32  }
    33  
    34  func (listener *AccountsListener) initialize(conf *config.Configuration, log *zerolog.Logger) error {
    35  	if conf == nil {
    36  		return errors.Errorf("no configuration provided")
    37  	}
    38  	listener.conf = conf
    39  
    40  	if log == nil {
    41  		return errors.Errorf("no logger provided")
    42  	}
    43  	listener.log = log
    44  
    45  	return nil
    46  }
    47  
    48  // AccountCreated is called whenever an account was created.
    49  func (listener *AccountsListener) AccountCreated(account *data.Account) {
    50  	listener.updateGOCDB(account, false)
    51  }
    52  
    53  // AccountUpdated is called whenever an account was updated.
    54  func (listener *AccountsListener) AccountUpdated(account *data.Account) {
    55  	listener.updateGOCDB(account, false)
    56  }
    57  
    58  // AccountRemoved is called whenever an account was removed.
    59  func (listener *AccountsListener) AccountRemoved(account *data.Account) {
    60  	listener.updateGOCDB(account, true)
    61  }
    62  
    63  func (listener *AccountsListener) updateGOCDB(account *data.Account, forceRemoval bool) {
    64  	if account != nil && account.Data.GOCDBAccess && !forceRemoval {
    65  		if err := writeAccount(account, opCreateOrUpdate, listener.conf.GOCDB.WriteURL, listener.conf.GOCDB.APIKey); err != nil {
    66  			listener.log.Err(err).Str("userid", account.Email).Msg("unable to update GOCDB account")
    67  		}
    68  	} else {
    69  		// Errors while deleting an account are ignored (account might not exist at all, for example)
    70  		_ = writeAccount(account, opDelete, listener.conf.GOCDB.WriteURL, listener.conf.GOCDB.APIKey)
    71  	}
    72  }
    73  
    74  // NewListener creates a new GOCDB accounts listener.
    75  func NewListener(conf *config.Configuration, log *zerolog.Logger) (*AccountsListener, error) {
    76  	listener := &AccountsListener{}
    77  	if err := listener.initialize(conf, log); err != nil {
    78  		return nil, errors.Wrap(err, "unable to initialize the GOCDB accounts listener")
    79  	}
    80  	return listener, nil
    81  }