1

Calculation to Determine Value for "svc":"user/update_user_flags" API

(17/06/2021 10:30:12 отредактировано arsalanqayum332)

Тема: Calculation to Determine Value for "svc":"user/update_user_flags" API

Dear Wialon Dev Support,

We are trying to understand and implement the following API:

API reference URL: https://sdk.wialon.com/wiki/en/sidebar/ … user_flags

The issue we’re facing is a simple understanding of proper calculation to determine from wialon api when Change Create Item, Can Change Password, Can Send SMS, Enable, Can Change Settings are Checked (enabled) or Unchecked (disabled).

Wialon API accepts a single digit value. This value can be calculated with the sum of all user permission property value’s shown in the screenshot below.

We are not able to calculate the sum. Below is our code example that we have come up with to calculate sum:

var user_flag = 0;
    var user_flag_mask = 0;
    if($("#can_create_items").prop("checked")){
            user_flag = + 4;
            user_flag_mask = +4;
    }
    if($("#can_change_password").prop("checked")){
            user_flag = + user_flag + 2;
            user_flag_mask = + user_flag_mask + 2;
    }
    if($("#can_send_sms").prop("checked")){
            user_flag = + user_flag + 0;
            user_flag_mask = + user_flag_mask + 0;
    }
    if($("#enable").prop("checked")){
            user_flag = + user_flag + 1;
            user_flag_mask = + user_flag_mask + 1;
    }
    if($("#can_change_settings").prop("checked")){
            user_flag = + user_flag + 16;
            user_flag_mask = + user_flag_mask + 16;
    }

If there is anyway you can edit our code above or a portion of it to help give us a better understanding, we would sincerely appreciate it. I don’t mean to ask you to do our homework, but we’re stuck.

I look forward to hearing from you.

  • Calculation to Determine Value for "svc":"user/update_user_flags" API
2

Calculation to Determine Value for "svc":"user/update_user_flags" API

Re: Calculation to Determine Value for "svc":"user/update_user_flags" API

Hello Arsalan!

user_flag_mask + 2  means  Can't change password ( no can change password)

can_send_sms - plus 32

user_flag = + user_flag + 1 - means user disable

user_flag = + user_flag + 16 - cannot chage settings

Please look here the answer https://forum.gurtam.com/viewtopic.php? … 62#p185362

Diana Cheley
Wialon Hosting Expert
Gurtam
3

Calculation to Determine Value for "svc":"user/update_user_flags" API

Re: Calculation to Determine Value for "svc":"user/update_user_flags" API

Also, maybe this example will be useful — https://sdk.wialon.com/playground/E3pLc5UZ/5

It's a encoder/decoder of all existing access flags.

Flags are combined with bitwise OR operator. In javascript it's | (vertical bar). But beware, because plain | works only on first 32 bits.

4

Calculation to Determine Value for "svc":"user/update_user_flags" API

(17/06/2021 10:29:52 отредактировано arsalanqayum332)

Re: Calculation to Determine Value for "svc":"user/update_user_flags" API

Dear Chdi and Rual,

Thank you both for your support. Really its helping, but we’re still stuck.

The encoder/decoder calculator is a huge help as we were totally unaware of its existence until now you provided the link.

Now the new problem is there are too many variables, how is it possible for us to determine all scenarios with this encoder/decoder?

We may end up reversing flags between 0 to 55

0x01 = 1 =    User disabled
0x02 = 2 =    Can't change password
0x04 = 4 =    Can create items
0x10 = 16 =    Can't change settings
0x20 = 32 =     Can send SMS

1+2+4+16+32 = 55

and so on the scenarios would be endless?

Based on the provided code snippet above how would you suggest we determine whether just these variables are checked or unchecked, then how we can edit just one, but not the other, and so on?
can_create_items,can_change_password,can_send_sms,enable,can_change_settings

Thank you again.

5

Calculation to Determine Value for "svc":"user/update_user_flags" API

Re: Calculation to Determine Value for "svc":"user/update_user_flags" API

Hello Arslan!

you need apply a corresponding mask and execute binary operation
If it needs to change bit from 1 to 0, the mask with zero bit and 1 for all, binary operation 'and' &
If it needs to change bit from 0 to 1, the mask with bit = 1 and 0 for all, binary operation 'or' |

For example, the option "Can change password" , to activate option it needs to apply bit2 = 0, so the mask is 1111 1101(in decimal 253) and binary operation 'and'
Current value flag 23 & 253 (mask) = 21 (0001 0101)

For example, the option 'Can send sms' - to activate option it needs bit6 convert from 0 to 1, so the mask is 0010 0000 (32) and binary operation 'or'
Current value flag 21 | 32 (mask) = 53

The all operation (in example above - two ones) are applied consistently.

To check bit status (0 or 1) it needs to apply mask with bitN=1 and all other 0, and  binary operation 'and' & :
if in result  all zeros, it means that bitN is 0, if in result bitN=1, so the bitN is activated
For example, current flag value 23 (1110 1000), we need to check option 'Enabled' - bit1 , so the mask is 0000 0001 (1)
23 & 1 = 1 - so the bit1 is activated which means User disabled (option 'Enabled' unchecked')
Or let check option 'Can change settings'  - bit6 , the mask is 0010 0000 (32)
23 & 32 = 0 - so the bit6 is zero, which means that option 'Can change settings'  activated.

P.S It's a little bit complicated as all options on interface are allowed (like can change, can send, enabled and etc), but the real flags don't have 1 status for allowed : for some options 0 is allowed, for other 1 is allowed.
PS.PS the binary format represented by a sequence of bits from right to left

Diana Cheley
Wialon Hosting Expert
Gurtam
6

Calculation to Determine Value for "svc":"user/update_user_flags" API

(17/06/2021 10:29:37 отредактировано arsalanqayum332)

Re: Calculation to Determine Value for "svc":"user/update_user_flags" API

Hi Chdi,

Thank you so much for the details above. I am able to make my way through bit wise 0, 1 and calculate flags and mask.