@@ -2281,6 +2281,8 @@ export class EntityCtrl extends entityCtrl<Base> {
22812281 for ( var i : number = this . keys . length - 1 ; i >= 0 ; i -- ) {
22822282 if ( this . keys [ i ] . startsWith ( '_' ) ) this . keys . splice ( i , 1 ) ;
22832283 }
2284+ this . searchSelectedItem = WebSocketClientService . user ;
2285+ this . adduser ( ) ;
22842286 this . processdata ( ) ;
22852287 //if (!this.$scope.$$phase) { this.$scope.$apply(); }
22862288 }
@@ -3606,4 +3608,299 @@ export class SocketsCtrl extends entitiesCtrl<Base> {
36063608 this . loading = false ;
36073609 this . loadData ( ) ;
36083610 }
3611+ }
3612+ export class CredentialsCtrl extends entitiesCtrl < Base > {
3613+ constructor (
3614+ public $scope : ng . IScope ,
3615+ public $location : ng . ILocationService ,
3616+ public $routeParams : ng . route . IRouteParamsService ,
3617+ public $interval : ng . IIntervalService ,
3618+ public WebSocketClientService : WebSocketClientService ,
3619+ public api : api ,
3620+ public userdata : userdata
3621+ ) {
3622+ super ( $scope , $location , $routeParams , $interval , WebSocketClientService , api , userdata ) ;
3623+ this . autorefresh = true ;
3624+ console . debug ( "CredentialsCtrl" ) ;
3625+ this . basequery = { _type : "credential" } ;
3626+ this . collection = "openrpa" ;
3627+ this . searchfields = [ "name" , "username" ] ;
3628+ this . postloadData = this . processData ;
3629+ if ( this . userdata . data . CredentialsCtrl ) {
3630+ this . basequery = this . userdata . data . CredentialsCtrl . basequery ;
3631+ this . collection = this . userdata . data . CredentialsCtrl . collection ;
3632+ this . baseprojection = this . userdata . data . CredentialsCtrl . baseprojection ;
3633+ this . orderby = this . userdata . data . CredentialsCtrl . orderby ;
3634+ this . searchstring = this . userdata . data . CredentialsCtrl . searchstring ;
3635+ this . basequeryas = this . userdata . data . CredentialsCtrl . basequeryas ;
3636+ }
3637+
3638+ WebSocketClientService . onSignedin ( ( user : TokenUser ) => {
3639+ this . loadData ( ) ;
3640+ } ) ;
3641+ }
3642+ async processData ( ) : Promise < void > {
3643+ if ( ! this . userdata . data . CredentialsCtrl ) this . userdata . data . CredentialsCtrl = { } ;
3644+ this . userdata . data . CredentialsCtrl . basequery = this . basequery ;
3645+ this . userdata . data . CredentialsCtrl . collection = this . collection ;
3646+ this . userdata . data . CredentialsCtrl . baseprojection = this . baseprojection ;
3647+ this . userdata . data . CredentialsCtrl . orderby = this . orderby ;
3648+ this . userdata . data . CredentialsCtrl . searchstring = this . searchstring ;
3649+ this . userdata . data . CredentialsCtrl . basequeryas = this . basequeryas ;
3650+ var chart : chartset = null ;
3651+ this . loading = false ;
3652+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
3653+ }
3654+ async DeleteOneUser ( model : TokenUser ) : Promise < any > {
3655+ this . loading = true ;
3656+ await NoderedUtil . DeleteOne ( this . collection , model . _id , null ) ;
3657+ this . models = this . models . filter ( function ( m : any ) : boolean { return m . _id !== model . _id ; } ) ;
3658+ this . loading = false ;
3659+ var name = model . username ;
3660+ name = name . split ( "@" ) . join ( "" ) . split ( "." ) . join ( "" ) ;
3661+ name = name . toLowerCase ( ) ;
3662+
3663+ var list = await NoderedUtil . Query ( "users" , { _type : "role" , name : name + "noderedadmins" } , null , null , 2 , 0 , null ) ;
3664+ if ( list . length == 1 ) {
3665+ console . debug ( "Deleting " + name + "noderedadmins" )
3666+ await NoderedUtil . DeleteOne ( "users" , list [ 0 ] . _id , null ) ;
3667+ }
3668+
3669+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
3670+ }
3671+ }
3672+ export class CredentialCtrl extends entityCtrl < Base > {
3673+ searchFilteredList : TokenUser [ ] = [ ] ;
3674+ searchSelectedItem : TokenUser = null ;
3675+ searchtext : string = "" ;
3676+ e : any = null ;
3677+ constructor (
3678+ public $scope : ng . IScope ,
3679+ public $location : ng . ILocationService ,
3680+ public $routeParams : ng . route . IRouteParamsService ,
3681+ public $interval : ng . IIntervalService ,
3682+ public WebSocketClientService : WebSocketClientService ,
3683+ public api : api
3684+ ) {
3685+ super ( $scope , $location , $routeParams , $interval , WebSocketClientService , api ) ;
3686+ console . debug ( "CredentialCtrl" ) ;
3687+ this . collection = "openrpa" ;
3688+ WebSocketClientService . onSignedin ( async ( user : TokenUser ) => {
3689+ if ( this . id !== null && this . id !== undefined ) {
3690+ await this . loadData ( ) ;
3691+ } else {
3692+ this . model = new Base ( ) ;
3693+ this . model . _type = "credential" ;
3694+ this . searchSelectedItem = WebSocketClientService . user ;
3695+ this . adduser ( ) ;
3696+ }
3697+ } ) ;
3698+ }
3699+ async submit ( ) : Promise < void > {
3700+ if ( this . model . _id ) {
3701+ await NoderedUtil . UpdateOne ( this . collection , null , this . model , 1 , false , null ) ;
3702+ } else {
3703+ await NoderedUtil . InsertOne ( this . collection , this . model , 1 , false , null ) ;
3704+ }
3705+ this . $location . path ( "/Credentials" ) ;
3706+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
3707+ }
3708+
3709+
3710+
3711+
3712+ removeuser ( _id ) {
3713+ if ( this . collection == "files" ) {
3714+ for ( var i = 0 ; i < ( this . model as any ) . metadata . _acl . length ; i ++ ) {
3715+ if ( ( this . model as any ) . metadata . _acl [ i ] . _id == _id ) {
3716+ ( this . model as any ) . metadata . _acl . splice ( i , 1 ) ;
3717+ }
3718+ }
3719+ } else {
3720+ for ( var i = 0 ; i < this . model . _acl . length ; i ++ ) {
3721+ if ( this . model . _acl [ i ] . _id == _id ) {
3722+ this . model . _acl . splice ( i , 1 ) ;
3723+ //this.model._acl = this.model._acl.splice(index, 1);
3724+ }
3725+ }
3726+ }
3727+
3728+ }
3729+ adduser ( ) {
3730+ var ace = new Ace ( ) ;
3731+ ace . deny = false ;
3732+ ace . _id = this . searchSelectedItem . _id ;
3733+ ace . name = this . searchSelectedItem . name ;
3734+ // ace.rights = "//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8=";
3735+ if ( this . WebSocketClientService . user . _id != ace . _id ) {
3736+ ace . rights = this . unsetBit ( ace . rights , 1 ) ;
3737+ ace . rights = this . setBit ( ace . rights , 2 ) ;
3738+ ace . rights = this . unsetBit ( ace . rights , 3 ) ;
3739+ ace . rights = this . unsetBit ( ace . rights , 4 ) ;
3740+ ace . rights = this . unsetBit ( ace . rights , 5 ) ;
3741+ }
3742+
3743+ if ( this . collection == "files" ) {
3744+ ( this . model as any ) . metadata . _acl . push ( ace ) ;
3745+ } else {
3746+ this . model . _acl . push ( ace ) ;
3747+ }
3748+ this . searchSelectedItem = null ;
3749+ this . searchtext = "" ;
3750+ }
3751+
3752+ isBitSet ( base64 : string , bit : number ) : boolean {
3753+ bit -- ;
3754+ var buf = this . _base64ToArrayBuffer ( base64 ) ;
3755+ var view = new Uint8Array ( buf ) ;
3756+ var octet = Math . floor ( bit / 8 ) ;
3757+ var currentValue = view [ octet ] ;
3758+ var _bit = ( bit % 8 ) ;
3759+ var mask = Math . pow ( 2 , _bit ) ;
3760+ return ( currentValue & mask ) != 0 ;
3761+ }
3762+ setBit ( base64 : string , bit : number ) {
3763+ bit -- ;
3764+ var buf = this . _base64ToArrayBuffer ( base64 ) ;
3765+ var view = new Uint8Array ( buf ) ;
3766+ var octet = Math . floor ( bit / 8 ) ;
3767+ var currentValue = view [ octet ] ;
3768+ var _bit = ( bit % 8 ) ;
3769+ var mask = Math . pow ( 2 , _bit ) ;
3770+ var newValue = currentValue | mask ;
3771+ view [ octet ] = newValue ;
3772+ return this . _arrayBufferToBase64 ( view ) ;
3773+ }
3774+ unsetBit ( base64 : string , bit : number ) {
3775+ bit -- ;
3776+ var buf = this . _base64ToArrayBuffer ( base64 ) ;
3777+ var view = new Uint8Array ( buf ) ;
3778+ var octet = Math . floor ( bit / 8 ) ;
3779+ var currentValue = view [ octet ] ;
3780+ var _bit = ( bit % 8 ) ;
3781+ var mask = Math . pow ( 2 , _bit ) ;
3782+ var newValue = currentValue &= ~ mask ;
3783+ view [ octet ] = newValue ;
3784+ return this . _arrayBufferToBase64 ( view ) ;
3785+ }
3786+ toogleBit ( a : any , bit : number ) {
3787+ if ( this . isBitSet ( a . rights , bit ) ) {
3788+ a . rights = this . unsetBit ( a . rights , bit ) ;
3789+ } else {
3790+ a . rights = this . setBit ( a . rights , bit ) ;
3791+ }
3792+ var buf2 = this . _base64ToArrayBuffer ( a . rights ) ;
3793+ var view2 = new Uint8Array ( buf2 ) ;
3794+ }
3795+ _base64ToArrayBuffer ( string_base64 ) : ArrayBuffer {
3796+ var binary_string = window . atob ( string_base64 ) ;
3797+ var len = binary_string . length ;
3798+ var bytes = new Uint8Array ( len ) ;
3799+ for ( var i = 0 ; i < len ; i ++ ) {
3800+ //var ascii = string_base64.charCodeAt(i);
3801+ var ascii = binary_string . charCodeAt ( i ) ;
3802+ bytes [ i ] = ascii ;
3803+ }
3804+ return bytes . buffer ;
3805+ }
3806+ _arrayBufferToBase64 ( array_buffer ) : string {
3807+ var binary = '' ;
3808+ var bytes = new Uint8Array ( array_buffer ) ;
3809+ var len = bytes . byteLength ;
3810+ for ( var i = 0 ; i < len ; i ++ ) {
3811+ binary += String . fromCharCode ( bytes [ i ] )
3812+ }
3813+ return window . btoa ( binary ) ;
3814+ }
3815+
3816+
3817+
3818+
3819+ restrictInput ( e ) {
3820+ if ( e . keyCode == 13 ) {
3821+ e . preventDefault ( ) ;
3822+ return false ;
3823+ }
3824+ }
3825+ setkey ( e ) {
3826+ this . e = e ;
3827+ this . handlekeys ( ) ;
3828+ }
3829+ handlekeys ( ) {
3830+ if ( this . searchFilteredList . length > 0 ) {
3831+ var idx : number = - 1 ;
3832+ for ( var i = 0 ; i < this . searchFilteredList . length ; i ++ ) {
3833+ if ( this . searchSelectedItem != null ) {
3834+ if ( this . searchFilteredList [ i ] . _id == this . searchSelectedItem . _id ) {
3835+ idx = i ;
3836+ }
3837+ }
3838+ }
3839+ if ( this . e . keyCode == 38 ) { // up
3840+ if ( idx <= 0 ) {
3841+ idx = 0 ;
3842+ } else { idx -- ; }
3843+ console . debug ( "idx: " + idx ) ;
3844+ // this.searchtext = this.searchFilteredList[idx].name;
3845+ this . searchSelectedItem = this . searchFilteredList [ idx ] ;
3846+ return ;
3847+ }
3848+ else if ( this . e . keyCode == 40 ) { // down
3849+ if ( idx >= this . searchFilteredList . length ) {
3850+ idx = this . searchFilteredList . length - 1 ;
3851+ } else { idx ++ ; }
3852+ console . debug ( "idx: " + idx ) ;
3853+ // this.searchtext = this.searchFilteredList[idx].name;
3854+ this . searchSelectedItem = this . searchFilteredList [ idx ] ;
3855+ return ;
3856+ }
3857+ else if ( this . e . keyCode == 13 ) { // enter
3858+ if ( idx >= 0 ) {
3859+ this . searchtext = this . searchFilteredList [ idx ] . name ;
3860+ this . searchSelectedItem = this . searchFilteredList [ idx ] ;
3861+ this . searchFilteredList = [ ] ;
3862+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
3863+ }
3864+ return ;
3865+ }
3866+ else {
3867+ // console.debug(this.e.keyCode);
3868+ }
3869+ } else {
3870+ if ( this . e . keyCode == 13 && this . searchSelectedItem != null ) {
3871+ this . adduser ( ) ;
3872+ }
3873+ }
3874+ }
3875+ async handlefilter ( e ) {
3876+ this . e = e ;
3877+ // console.debug(e.keyCode);
3878+ var ids : string [ ] ;
3879+ if ( this . collection == "files" ) {
3880+ ids = ( this . model as any ) . metadata . _acl . map ( item => item . _id ) ;
3881+ } else {
3882+ ids = this . model . _acl . map ( item => item . _id ) ;
3883+ }
3884+ this . searchFilteredList = await NoderedUtil . Query ( "users" ,
3885+ {
3886+ $and : [
3887+ { $or : [ { _type : "user" } , { _type : "role" } ] } ,
3888+ { name : new RegExp ( [ this . searchtext ] . join ( "" ) , "i" ) } ,
3889+ { _id : { $nin : ids } }
3890+ ]
3891+ }
3892+ , null , { _type : - 1 , name : 1 } , 5 , 0 , null ) ;
3893+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
3894+ }
3895+ fillTextbox ( searchtext ) {
3896+ this . searchFilteredList . forEach ( ( item : any ) => {
3897+ if ( item . name . toLowerCase ( ) == searchtext . toLowerCase ( ) ) {
3898+ this . searchtext = item . name ;
3899+ this . searchSelectedItem = item ;
3900+ this . searchFilteredList = [ ] ;
3901+ if ( ! this . $scope . $$phase ) { this . $scope . $apply ( ) ; }
3902+ }
3903+ } ) ;
3904+ }
3905+
36093906}
0 commit comments