2017-07-26 10:50:26 +01:00
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package mysql
2017-07-24 16:24:21 +01:00
import (
"database/sql"
"fmt"
"time"
2017-07-26 10:50:26 +01:00
"github.com/documize/community/core/env"
2017-07-24 16:24:21 +01:00
"github.com/documize/community/domain"
"github.com/documize/community/domain/store/mysql"
2017-07-26 10:50:26 +01:00
"github.com/documize/community/model/account"
2017-07-24 16:24:21 +01:00
"github.com/pkg/errors"
)
2017-07-26 10:50:26 +01:00
// Scope provides data access to MySQL.
type Scope struct {
Runtime * env . Runtime
}
2017-07-24 16:24:21 +01:00
// Add inserts the given record into the datbase account table.
2017-07-26 10:50:26 +01:00
func ( s Scope ) Add ( ctx domain . RequestContext , account account . Account ) ( err error ) {
2017-07-24 16:24:21 +01:00
account . Created = time . Now ( ) . UTC ( )
account . Revised = time . Now ( ) . UTC ( )
2017-09-25 14:37:11 +01:00
_ , err = ctx . Transaction . Exec ( "INSERT INTO account (refid, orgid, userid, admin, editor, users, active, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)" ,
account . RefID , account . OrgID , account . UserID , account . Admin , account . Editor , account . Users , account . Active , account . Created , account . Revised )
2017-07-24 16:24:21 +01:00
if err != nil {
err = errors . Wrap ( err , "unable to execute insert for account" )
}
return
}
// GetUserAccount returns the database account record corresponding to the given userID, using the client's current organizaion.
2017-07-26 10:50:26 +01:00
func ( s Scope ) GetUserAccount ( ctx domain . RequestContext , userID string ) ( account account . Account , err error ) {
2017-09-25 11:52:26 +01:00
err = s . Runtime . Db . Get ( & account , `
2017-10-03 17:52:03 -04:00
SELECT a . id , a . refid , a . orgid , a . userid , a . editor , a . admin , a . users , a . active , a . created , a . revised ,
2017-09-25 11:52:26 +01:00
b . company , b . title , b . message , b . domain
FROM account a , organization b
WHERE b . refid = a . orgid AND a . orgid = ? AND a . userid = ? ` , ctx . OrgID , userID )
2017-07-24 16:24:21 +01:00
if err != sql . ErrNoRows && err != nil {
err = errors . Wrap ( err , fmt . Sprintf ( "execute select for account by user %s" , userID ) )
}
return
}
// GetUserAccounts returns a slice of database account records, for all organizations that the userID is a member of, in organization title order.
2017-07-26 10:50:26 +01:00
func ( s Scope ) GetUserAccounts ( ctx domain . RequestContext , userID string ) ( t [ ] account . Account , err error ) {
2017-09-25 09:27:04 +01:00
err = s . Runtime . Db . Select ( & t , `
2017-10-03 17:52:03 -04:00
SELECT a . id , a . refid , a . orgid , a . userid , a . editor , a . admin , a . users , a . active , a . created , a . revised ,
2017-09-25 09:27:04 +01:00
b . company , b . title , b . message , b . domain
FROM account a , organization b
WHERE a . userid = ? AND a . orgid = b . refid AND a . active = 1 ORDER BY b . title ` , userID )
2017-07-24 16:24:21 +01:00
if err != sql . ErrNoRows && err != nil {
err = errors . Wrap ( err , fmt . Sprintf ( "Unable to execute select account for user %s" , userID ) )
}
return
}
// GetAccountsByOrg returns a slice of database account records, for all users in the client's organization.
2017-07-26 10:50:26 +01:00
func ( s Scope ) GetAccountsByOrg ( ctx domain . RequestContext ) ( t [ ] account . Account , err error ) {
2017-09-25 09:27:04 +01:00
err = s . Runtime . Db . Select ( & t ,
2017-10-03 17:52:03 -04:00
` SELECT a . id , a . refid , a . orgid , a . userid , a . editor , a . admin , a . users , a . active , a . created , a . revised ,
2017-09-25 11:52:26 +01:00
b . company , b . title , b . message , b . domain
2017-09-25 09:27:04 +01:00
FROM account a , organization b
WHERE a . orgid = b . refid AND a . orgid = ? AND a . active = 1 ` , ctx . OrgID )
2017-07-24 16:24:21 +01:00
if err != sql . ErrNoRows && err != nil {
2017-07-26 10:50:26 +01:00
err = errors . Wrap ( err , fmt . Sprintf ( "execute select account for org %s" , ctx . OrgID ) )
2017-07-24 16:24:21 +01:00
}
return
}
// CountOrgAccounts returns the numnber of active user accounts for specified organization.
2017-07-26 10:50:26 +01:00
func ( s Scope ) CountOrgAccounts ( ctx domain . RequestContext ) ( c int ) {
row := s . Runtime . Db . QueryRow ( "SELECT count(*) FROM account WHERE orgid=? AND active=1" , ctx . OrgID )
2017-07-24 16:24:21 +01:00
err := row . Scan ( & c )
if err == sql . ErrNoRows {
return 0
}
if err != nil {
err = errors . Wrap ( err , "count org accounts" )
return 0
}
return
}
// UpdateAccount updates the database record for the given account to the given values.
2017-07-26 10:50:26 +01:00
func ( s Scope ) UpdateAccount ( ctx domain . RequestContext , account account . Account ) ( err error ) {
2017-07-24 16:24:21 +01:00
account . Revised = time . Now ( ) . UTC ( )
2017-09-25 14:37:11 +01:00
_ , err = ctx . Transaction . NamedExec ( "UPDATE account SET userid=:userid, admin=:admin, editor=:editor, users=:users, active=:active, revised=:revised WHERE orgid=:orgid AND refid=:refid" , & account )
2017-07-24 16:24:21 +01:00
if err != sql . ErrNoRows && err != nil {
err = errors . Wrap ( err , fmt . Sprintf ( "execute update for account %s" , account . RefID ) )
}
return
}
// HasOrgAccount returns if the given orgID has valid userID.
2017-07-26 10:50:26 +01:00
func ( s Scope ) HasOrgAccount ( ctx domain . RequestContext , orgID , userID string ) bool {
2017-07-24 16:24:21 +01:00
row := s . Runtime . Db . QueryRow ( "SELECT count(*) FROM account WHERE orgid=? and userid=?" , orgID , userID )
var count int
err := row . Scan ( & count )
if err == sql . ErrNoRows {
return false
}
if err != nil && err != sql . ErrNoRows {
err = errors . Wrap ( err , "HasOrgAccount" )
return false
}
if count == 0 {
return false
}
return true
}
// DeleteAccount deletes the database record in the account table for user ID.
2017-07-26 10:50:26 +01:00
func ( s Scope ) DeleteAccount ( ctx domain . RequestContext , ID string ) ( rows int64 , err error ) {
2017-07-24 16:24:21 +01:00
b := mysql . BaseQuery { }
2017-07-26 10:50:26 +01:00
return b . DeleteConstrained ( ctx . Transaction , "account" , ctx . OrgID , ID )
2017-07-24 16:24:21 +01:00
}