2014-10-20 19:52:35 +02:00
( function ( ) {
'use strict' ;
2014-11-03 18:19:35 +01:00
/ * *
* @ name User
* @ constructor
* @ param { object } [ userData ] - some default values for the user
* /
function User ( userData ) {
2015-04-20 23:28:43 +02:00
if ( userData ) {
2015-05-06 23:45:28 +02:00
this . init ( userData ) ;
2015-04-20 23:28:43 +02:00
}
2014-11-03 18:19:35 +01:00
}
2014-10-20 19:52:35 +02:00
2014-11-03 18:19:35 +01:00
/ * *
* @ memberof User
* @ desc The factory we ' ll use to register with Angular .
* @ return the User constructor
* /
2016-06-22 03:29:44 +02:00
User . factory = [ '$q' , '$log' , 'sgSettings' , 'Resource' , function ( $q , $log , Settings , Resource ) {
2014-10-20 19:52:35 +02:00
angular . extend ( User , {
2014-11-03 18:19:35 +01:00
$q : $q ,
2015-05-19 20:50:26 +02:00
$log : $log ,
2015-08-03 17:53:54 +02:00
$$resource : new Resource ( Settings . activeUser ( 'folderURL' ) , Settings . activeUser ( ) ) ,
2015-08-06 17:18:53 +02:00
$query : '' ,
$users : [ ]
2014-10-20 19:52:35 +02:00
} ) ;
2014-11-03 18:19:35 +01:00
return User ;
2014-10-20 19:52:35 +02:00
} ] ;
2014-11-03 18:19:35 +01:00
/ * *
* @ module SOGo . Common
* @ desc Factory registration of User in Angular module .
* /
2015-05-06 04:06:13 +02:00
angular . module ( 'SOGo.Common' ) . factory ( 'User' , User . factory ) ;
2014-10-20 19:52:35 +02:00
2014-11-03 18:19:35 +01:00
/ * *
* @ memberof User
* @ desc Search for users that match a string .
2014-12-12 19:39:02 +01:00
* @ param { string } search - a string used to performed the search
2015-08-05 22:44:25 +02:00
* @ param { object [ ] } excludedUsers - a list of User objects that must be excluded from the results
2014-11-03 18:19:35 +01:00
* @ return a promise of an array of matching User objects
2014-10-20 19:52:35 +02:00
* /
2015-10-09 22:10:39 +02:00
User . $filter = function ( search , excludedUsers , options ) {
2015-10-08 16:04:25 +02:00
var _this = this , param = { search : search } ;
2015-05-07 21:15:20 +02:00
2015-10-09 22:10:39 +02:00
if ( ! options || ! options . dry ) {
if ( ! search ) {
// No query specified
User . $users . splice ( 0 , User . $users . length ) ;
return User . $q . when ( User . $users ) ;
}
if ( User . $query == search ) {
// Query hasn't changed
return User . $q . when ( User . $users ) ;
}
User . $query = search ;
2015-05-19 20:50:26 +02:00
}
2015-05-07 21:15:20 +02:00
2015-07-07 22:34:58 +02:00
return User . $$resource . fetch ( null , 'usersSearch' , param ) . then ( function ( response ) {
2015-10-09 22:10:39 +02:00
var results , index , user , users ,
2015-07-24 22:14:53 +02:00
compareUids = function ( data ) {
2015-10-08 16:04:25 +02:00
return this . uid == data . uid ;
2015-07-24 22:14:53 +02:00
} ;
2015-10-09 22:10:39 +02:00
2016-02-29 21:13:09 +01:00
if ( options ) {
if ( options . dry )
users = [ ] ;
else if ( options . results )
users = options . results ;
}
2015-10-09 22:10:39 +02:00
else
users = User . $users ;
2015-05-27 20:59:49 +02:00
if ( excludedUsers ) {
// Remove excluded users from response
2015-10-08 16:04:25 +02:00
results = _ . filter ( response . users , function ( user ) {
2016-03-03 19:38:54 +01:00
return ! _ . find ( excludedUsers , _ . bind ( compareUids , user ) ) ;
2015-05-27 20:59:49 +02:00
} ) ;
}
else {
results = response . users ;
}
2015-10-08 16:04:25 +02:00
2015-05-19 20:50:26 +02:00
// Remove users that no longer match the search query
2015-10-09 22:10:39 +02:00
for ( index = users . length - 1 ; index >= 0 ; index -- ) {
user = users [ index ] ;
2016-03-03 19:38:54 +01:00
if ( ! _ . find ( results , _ . bind ( compareUids , user ) ) ) {
2015-10-09 22:10:39 +02:00
users . splice ( index , 1 ) ;
2015-05-19 20:50:26 +02:00
}
}
2015-08-06 17:18:53 +02:00
// Add new users matching the search query
2016-03-03 19:38:54 +01:00
_ . forEach ( results , function ( data , index ) {
if ( _ . isUndefined ( _ . find ( users , _ . bind ( compareUids , data ) ) ) ) {
2015-08-06 17:18:53 +02:00
var user = new User ( data ) ;
2015-10-09 22:10:39 +02:00
users . splice ( index , 0 , user ) ;
2015-08-06 17:18:53 +02:00
}
} ) ;
2015-10-09 22:10:39 +02:00
User . $log . debug ( users ) ;
return users ;
2015-07-07 22:34:58 +02:00
} ) ;
2014-11-03 18:19:35 +01:00
} ;
2015-05-06 23:45:28 +02:00
/ * *
* @ function init
* @ memberof User . prototype
* @ desc Extend instance with required attributes and new data .
* @ param { object } data - attributes of user
* /
User . prototype . init = function ( data ) {
angular . extend ( this , data ) ;
2015-05-07 16:04:50 +02:00
if ( ! this . $$shortFormat )
this . $$shortFormat = this . $shortFormat ( ) ;
if ( ! this . $$image )
2016-06-22 03:29:44 +02:00
this . $$image = this . image ;
// NOTE: We can't assign a Gravatar at this stage since we would need the Preferences module
// which already depend on the User module.
2015-07-28 16:53:29 +02:00
// An empty attribute to trick md-autocomplete when adding users from the ACLs editor
this . empty = ' ' ;
2015-05-06 23:45:28 +02:00
} ;
2014-11-03 18:19:35 +01:00
/ * *
* @ function $shortFormat
* @ memberof User . prototype
* @ return the fullname along with the email address
* /
2014-11-25 22:09:55 +01:00
User . prototype . $shortFormat = function ( options ) {
2014-11-03 18:19:35 +01:00
var fullname = this . cn || this . c _email ;
2014-12-12 19:39:02 +01:00
var email = this . c _email ;
2014-11-25 22:09:55 +01:00
var no _email = options && options . email === false ;
2014-12-12 19:39:02 +01:00
if ( ! no _email && email && fullname != email ) {
2015-04-20 23:28:43 +02:00
fullname += ' <' + email + '>' ;
2014-11-03 18:19:35 +01:00
}
return fullname ;
} ;
/ * *
* @ function $acl
* @ memberof User . prototype
* @ desc Fetch the user rights associated to a specific folder and populate the 'rights' attribute .
2014-12-12 19:39:02 +01:00
* @ param { string } the folder ID
2015-10-06 22:38:16 +02:00
* @ param { Object } owner - the owner to use when fetching the ACL as it might not be the Settings . activeUser
2014-11-03 18:19:35 +01:00
* @ return a promise
* /
2015-10-06 22:38:16 +02:00
User . prototype . $acl = function ( folderId , owner ) {
2014-11-03 18:19:35 +01:00
var _this = this ,
deferred = User . $q . defer ( ) ,
param = { uid : this . uid } ;
if ( this . $shadowRights ) {
deferred . resolve ( this . rights ) ;
}
else {
2015-10-06 22:38:16 +02:00
var rights ;
if ( angular . isDefined ( owner ) )
rights = User . $$resource . userResource ( owner ) . fetch ( folderId , 'userRights' , param ) ;
else
rights = User . $$resource . fetch ( folderId , 'userRights' , param ) ;
rights . then ( function ( data ) {
2014-11-03 18:19:35 +01:00
_this . rights = data ;
// Convert numbers (0|1) to boolean values
2015-04-24 22:07:48 +02:00
//angular.forEach(_.keys(_this.rights), function(key) {
// _this.rights[key] = _this.rights[key] ? true : false;
//});
2014-11-03 18:19:35 +01:00
// console.debug('rights ' + _this.uid + ' => ' + JSON.stringify(data, undefined, 2));
// Keep a copy of the server's version
_this . $shadowRights = angular . copy ( data ) ;
deferred . resolve ( data ) ;
return data ;
} ) ;
}
2014-11-11 17:14:55 +01:00
return deferred . promise ;
2014-11-03 18:19:35 +01:00
} ;
/ * *
* @ function $isAnonymous
* @ memberof User . prototype
* @ return true if it ' s the special anonymous user
* /
User . prototype . $isAnonymous = function ( ) {
return this . uid == 'anonymous' ;
} ;
/ * *
* @ function $isSpecial
* @ memberof User . prototype
2015-08-06 17:18:53 +02:00
* @ desc Only accurate from the ACL editor .
2014-11-03 18:19:35 +01:00
* @ return true if the user is not a regular system user
* /
User . prototype . $isSpecial = function ( ) {
return this . userClass && this . userClass == 'public-user' ;
} ;
/ * *
* @ function $confirmRights
* @ memberof User . prototype
* @ desc Check if a confirmation is required before giving some rights .
* @ return the confirmation message or false if no confirmation is required
* /
2016-02-23 16:20:10 +01:00
User . prototype . $confirmRights = function ( folder ) {
2014-11-03 18:19:35 +01:00
var confirmation = false ;
if ( this . $confirmation ) {
// Don't bother the user more than once
return false ;
}
if ( _ . some ( _ . values ( this . rights ) ) ) {
if ( this . uid == 'anonymous' ) {
2016-02-23 16:20:10 +01:00
if ( folder . constructor . name == 'AddressBook' )
confirmation = l ( 'Potentially anyone on the Internet will be able to access your address book "%{0}", even if they do not have an account on this system. Is this information suitable for the public Internet?' , folder . name ) ;
else if ( folder . constructor . name == 'Calendar' )
confirmation = l ( 'Potentially anyone on the Internet will be able to access your calendar "%{0}", even if they do not have an account on this system. Is this information suitable for the public Internet?' , folder . name ) ;
2014-11-03 18:19:35 +01:00
}
2016-02-23 16:20:10 +01:00
else if ( this . uid == 'anyone' || this . uid == '<default>' ) {
if ( folder . constructor . name == 'AddressBook' )
confirmation = l ( 'Any user with an account on this system will be able to access your address book "%{0}". Are you certain you trust them all?' , folder . name ) ;
else if ( folder . constructor . name == 'Calendar' )
confirmation = l ( 'Any user with an account on this system will be able to access your calendar "%{0}". Are you certain you trust them all?' , folder . name ) ;
else if ( folder . constructor . name == 'Mailbox' )
confirmation = l ( 'Any user with an account on this system will be able to access your mailbox "%{0}". Are you certain you trust them all?' , folder . name ) ;
2014-11-03 18:19:35 +01:00
}
}
this . $confirmation = confirmation ;
return confirmation ;
} ;
/ * *
* @ function $rightsAreDirty
* @ memberof User . prototype
* @ return whether or not the rights have changed from their initial values
* /
User . prototype . $rightsAreDirty = function ( ) {
return this . rights && ! _ . isEqual ( this . rights , this . $shadowRights ) ;
2014-10-20 19:52:35 +02:00
} ;
2014-11-03 18:19:35 +01:00
/ * *
* @ function $resetRights
* @ memberof User . prototype
* @ desc Restore initial rights or disable all rights
2014-12-12 19:39:02 +01:00
* @ param { boolean } [ zero ] - reset all rights to zero when true
2014-11-03 18:19:35 +01:00
* /
User . prototype . $resetRights = function ( zero ) {
var _this = this ;
if ( zero ) {
// Disable all rights
_ . map ( _ . keys ( this . rights ) , function ( key ) {
2015-06-02 17:14:03 +02:00
if ( angular . isString ( _this . rights [ key ] ) )
_this . rights [ key ] = 'None' ;
else
_this . rights [ key ] = 0 ;
2014-11-03 18:19:35 +01:00
} ) ;
}
2016-09-23 22:43:48 +02:00
else if ( this . $shadowRights ) {
2014-11-03 18:19:35 +01:00
// Restore initial rights
this . rights = angular . copy ( this . $shadowRights ) ;
}
} ;
2014-11-11 17:14:55 +01:00
/ * *
* @ function $folders
* @ memberof User . prototype
2014-12-12 19:39:02 +01:00
* @ desc Retrieve the list of folders of a specific type
* @ param { string } type - either 'contact' or 'calendar'
2014-11-11 17:14:55 +01:00
* @ return a promise of the HTTP query result or the cached result
* /
User . prototype . $folders = function ( type ) {
var _this = this ,
deferred = User . $q . defer ( ) ,
param = { type : type } ;
if ( this . $$folders ) {
deferred . resolve ( this . $$folders ) ;
}
else {
2015-01-13 17:30:23 +01:00
User . $$resource . userResource ( this . uid ) . fetch ( null , 'foldersSearch' , param ) . then ( function ( response ) {
_this . $$folders = response . folders ;
deferred . resolve ( response . folders ) ;
2014-11-11 17:14:55 +01:00
} ) ;
}
return deferred . promise ;
} ;
2014-11-03 18:19:35 +01:00
/ * *
* @ function $omit
* @ memberof User . prototype
* @ desc Return a sanitized object used to send to the server .
* @ return an object literal copy of the User instance
* /
User . prototype . $omit = function ( ) {
var user = { } ;
angular . forEach ( this , function ( value , key ) {
if ( key != 'constructor' && key [ 0 ] != '$' ) {
user [ key ] = value ;
}
} ) ;
return user ;
} ;
2015-07-24 22:14:53 +02:00
User . prototype . toString = function ( ) {
return '[User ' + this . c _email + ']' ;
} ;
2014-10-08 20:08:54 +02:00
} ) ( ) ;