Loading and saving users and groups

Previous: Checking for existence of users and groups ] [ Top: The user module ] [ Next: Adding and removing users and things from groups ]

Given the simplicity of the xmlapi, most of this is pretty easy. However, the fact that we're using the user module to authorize access to users makes it interestingly recursive. But looking at the auth versions of the get and save functions, we can see an excellent example of how to use the module.

user_get()
 
XML * user_get (const char * username)
{
   char buf[1024];
   FILE * file;
   XML * ret;

   if (!strcmp (username, "anonymous")) {
      ret = xml_create ("user");
      xml_set (ret, "name", "anonymous");
      return (ret);
   }

   sprintf (buf, "%s%s.xml", USER_REPOSITORY, username);
   file = fopen (buf, "r");
   if (!file) return NULL;

   ret = xml_read (file);
   fclose (file);
   return (ret);
}


user_save()
 
int user_save (XML * user)
{
   char buf[1024];
   FILE * file;

   sprintf (buf, "%s%s.xml", USER_REPOSITORY, xml_attrval (user, "name"));
   file = fopen (buf, "w+");
   if (!file) return 1;

   xml_write (file, user);
   fclose (file);
   return (0);
}


group_get()
 
XML * group_get (const char * groupname)
{
   char buf[1024];
   FILE * file;
   XML * ret;

   sprintf (buf, "%s%s.xml", GROUP_REPOSITORY, groupname);
   file = fopen (buf, "r");
   if (!file) {
      ret = xml_create ("group");
      xml_set (ret, "name", groupname);
      return (ret);
   }

   ret = xml_read (file);
   fclose (file);
   return (ret);
}


group_save()
 
int group_save (XML * group)
{
   char buf[1024];
   FILE * file;

   sprintf (buf, "%s%s.xml", GROUP_REPOSITORY, xml_attrval (group, "name"));
   file = fopen (buf, "w+");
   if (!file) return 1;

   xml_write (file, group);
   fclose (file);
   return (0);
}


user_auth_get()


user_auth_save()


group_auth_get()


group_auth_save()
Previous: Checking for existence of users and groups ] [ Top: The user module ] [ Next: Adding and removing users and things from groups ]


This code and documentation are released under the terms of the GNU license. They are additionally copyright (c) 2000, Vivtek. All rights reserved except those explicitly granted under the terms of the GNU license.