#include <stdio.h>
#include <string.h>

#include "../global.h"
#include "shared_lock.h"
#include "shared_person_list.h"
#include "shared_access.h"
#include "shared_cs_util.h"


void
get_access_rights(GROUP_TYPE group, int access, int *can_view, int *can_write)
{

 *can_view = 0;
 *can_write = 0;
 
 if(group == ADMIN)
  {
   *can_view = 1;
   *can_write = 1;
  }
 else
  {    
     switch(access)
     {
      case ACCESS_ALL:                *can_view = 1;
                                      *can_write = 1;
                                       break;
                       
      case ACCESS_NONE:               break;

      case ACCESS_TEACHER_READ:       if(group == FACULTY)
                                        *can_view = 1;             
                                        
                                      break;
                                      
      case ACCESS_ALL_READ:           *can_view = 1;
                                      break;
                                      
      case ACCESS_TEACHER:           if(group == FACULTY)
                                      {
                                       *can_view = 1;
                                       *can_write = 1;
                                      }
                                      break;

      case ACCESS_STUDENT_READ:      if(group == FACULTY)
                                      {
                                       *can_view = 1;
                                       *can_write = 1;
                                      }
                                      else
                                        *can_view = 1;
                                      break;
                                      
      case ACCESS_CAPTURE:            break;    /* what to do here? */
      
      default:                        break;
     }
  }
 
} 
                                       


void
access_denied_error()
{
 cs_critical_error(ERR_REQUEST_DENIED, "");
} 
 


void 
get_username_access_rights(const char *username, CONFIG_STRUCT *conf, int *can_view, int *can_write)
{
 char passwd_fname[MAX_PATH + 1];
 int username_found = 0;
 char line[201];
 USER_DATA person;    
 FILE *fp;
  
 *can_view = 0;
 *can_write = 0;

 if(conf->access == ACCESS_ALL)
  {
   *can_view = 1;
   *can_write = 1;
  }
     
 else if(conf->access != ACCESS_NONE)  /* we need to figure out if this is a student or a teacher */
  {    
   snprintf(passwd_fname, MAX_PATH + 1, "%s%s", conf->course_path, PASSWD_FNAME);
   fp = fopen(passwd_fname, "r");
   if(!fp)
      return;
  
    get_shared_lock(fileno(fp),1);   /* shared_lock.c */
    while (!username_found && fgets (line, 200, fp))
     {
     if (*line == '?')		/* password lines starting with a '?' are ignored */
        continue;
     get_passwd_line_data(line, &person);   /* shared_person_list.c */
     if(!strcmp(person.username, username))
       {
         username_found = 1;
         get_access_rights(person.group, conf->access, can_view, can_write);
       } 
     }
     
     release_lock(fileno(fp));   /* shared_lock.c */
     fclose(fp);
 }
}
  
  
  
/* is user set to be 'capturing' a classroom? */
int user_capturing(SESSION *user)
{
 return !strcmp(user->alias, ALIAS_CAPTURE_FLAG);    /* shared_access.h */
} 
  
 



int
has_write_permission(SESSION *user, CONFIG_STRUCT *conf)
{
 
 int can_view;
 int can_write;
 
 get_access_rights(user->group, conf->access, &can_view, &can_write);
 if(!can_view && !user_capturing(user))
        access_denied_error();
 return can_write;
}








