anchor.diff:  -include_lib("eunit/include/eunit.hrl").
   
   -record(state, {buckets,
  -                users,
                   roles,
                   admin_user}).
   
  
positive.code: memcached_admin_json(AU, Buckets) ->
      jsonify_user({AU, builtin}, [{global, [all]} | [{Name, [all]} || Name <- Buckets]]).
  
  generate_json(Buckets, RoleDefinitions, Users) ->
      RolesDict = dict:new(),
      BucketUsers = [{{Name, builtin}, menelaus_roles:get_roles({Name, bucket})} ||
                        Name <- Buckets],
      {Json, _} =
          lists:foldl(fun ({Identity, Roles}, {Acc, Dict}) ->
                              {Permissions, NewDict} =
                                  permissions_for_user(Roles, Buckets, RoleDefinitions, Dict),
                              {[jsonify_user(Identity, Permissions) | Acc], NewDict}
                      end, {[], RolesDict}, BucketUsers ++ Users),
      lists:reverse(Json).
  
  generate_json_test() ->
      Buckets = ["default", "test"],
      Users = [{{"ivanivanov", builtin}, [{views_admin, ["default"]}, {bucket_admin, ["test"]}]},
               {{"petrpetrov", saslauthd}, [admin]}],
      RoleDefinitions = menelaus_roles:preconfigured_roles(),
  
      Json =
  
negative.code: init() ->
      Config = ns_config:get(),
      #state{buckets = ns_bucket:get_bucket_names(ns_bucket:get_buckets(Config)),
             admin_user = ns_config:search_node_prop(Config, memcached, admin_user),
             roles = menelaus_roles:get_definitions(Config)}.
  
  filter_event({buckets, _V}) ->
      true;
  filter_event({roles_definitions, _V}) ->
      true;
  filter_event({user_version, _V}) ->
      true;
  filter_event(_) ->
      false.
  