tags:

views:

27

answers:

3

Hello,

Let's imagine that we have privacy options page in social network; two group of radio buttons.

Allow to post on wall  p  f  c  (groupA)
Allow to view wall     p  f  c  (groupB)

p = public
f = only friends
c = closed

It is obvious that there is a dependency between this groups of checkboxes. For example, we should automatically set groupA=c when groupB=c; viewing wall is closed, so wall comments form should also be closed and so on.

It is possible to solve this problem using numerous if's, but we will have very complex control structure as result.

Any good solution?

Thank you

+1  A: 

You have 2 sets of permissions, and 'write' permissions should never be less restrictive than read. If (0 - no access, 1- limited[friends only], 2 - public access), then after changing value in GroupB validating GroupA value may look like GroupA.value = (GroupA.value <= GroupB.value) ? GroupA.value : GroupB.value. GroupB - read permissions, GroupA - write permissions.

a1ex07
A: 

Define one bit-mask for viewing, and another bitmask for posting, with one bit in each for public and friends (closed simply means both bits are set to 0). A bit that's set to 1 allows access, and a bit that's set to 0 denies access.

AND the "post" bitmask with the "view" bitmask to ensure that all the bits that are cleared in the "view" bitmask are also cleared in the "post" bitmask.

In something like C or C++, this would look something like this:

unsigned view;
unsigned post;

enum { friends = 1, public = 2 };

view = friends;
post = friends | public;    // create an invalid combination
post &= view;               // correct the invalid combination;
Jerry Coffin
A: 

You can also define the comparisions in a structure and check every entry in a function.

I mean something like that in C:

#define ACCESS_CLOSED 0
#define ACCESS_FRIEND 1
#define ACCESS_PUBLIC 2

typedef struct dep {
    int *master;
    int masterval;
    int *slave;
    int slaveval;
} dep_t;

int checkdeps(dep_t *deps, int n)
{
    int i;

    for (i=0; i<n; i++) {
        if (*(deps[i].master) == deps[i].masterval)
            *(deps[i].slave) = deps[i].slaveval;
    }
}

int main(void)
{
    int groupA = ACCESS_FRIEND;
    int groupB = ACCESS_FRIEND;
    int groupC = ACCESS_FRIEND;

    // if  the first argument has the value of the second argument
    // then the third is set to the value from the fourth
    dep_t deps[] = {
        { &groupB, ACCESS_CLOSED, &groupA, ACCESS_CLOSED },
        { &groupB, ACCESS_FRIEND, &groupC, ACCESS_CLOSED }
    };

    groupB = ACCESS_CLOSED;
    checkdeps(deps, sizeof(deps)/sizeof(dep_t));

    printf("A: %d, B: %d, C: %d\n", groupA, groupB, groupC);

    return 0;
}
rudi-moore