ffead.server.doc
FileAuthController.cpp
1 /*
2  * FileAuthController.cpp
3  *
4  * Created on: Nov 23, 2010
5  * Author: sumeet
6  */
7 
8 #include "FileAuthController.h"
9 
10 FileAuthController::FileAuthController(string filename,string delimiter) {
11  this->filename = filename;
12  this->delimiter = delimiter;
13 }
14 
15 FileAuthController::~FileAuthController() {
16  // TODO Auto-generated destructor stub
17 }
18 
19 string FileAuthController::treat_password(string password)
20 {
21  return password;
22 }
23 
24 bool FileAuthController::isInitialized()
25 {
26  ifstream ifs(this->filename.c_str());
27  bool fl = ifs.is_open();
28  ifs.close();
29  return fl;
30 }
31 
32 bool FileAuthController::authenticate(string username,string password)
33 {
34  password = treat_password(password);
35  string userstamp = (username + delimiter + password);
36  ifstream ifs(this->filename.c_str());
37  if(ifs.is_open())
38  {
39  string temp;
40  while(getline(ifs, temp))
41  {
42  if(temp.find(userstamp)!=string::npos)
43  {
44  ifs.close();
45  return true;
46  }
47  }
48  ifs.close();
49  }
50  return false;
51 }
52 
53 bool FileAuthController::authenticateSecurity(string username,string password)
54 {
55  password = treat_password(password);
56  ifstream ifs(this->filename.c_str());
57  if(ifs.is_open())
58  {
59  string temp;
60  while(getline(ifs, temp))
61  {
62  vector<string> tempv;
63  StringUtil::split(tempv, temp, (this->delimiter));
64  if(tempv.size()>=2 && tempv.at(0)==username && tempv.at(1)==password)
65  {
66  ifs.close();
67  return true;
68  }
69  }
70  ifs.close();
71  }
72  return false;
73 }
74 
75 bool FileAuthController::getPassword(string username,string &passwd)
76 {
77  bool passwdf = false;
78  ifstream ifs(this->filename.c_str());
79  if(ifs.is_open() && username!="")
80  {
81  string temp;
82  while(getline(ifs, temp))
83  {
84  vector<string> tempv;
85  StringUtil::split(tempv, temp, (this->delimiter));
86  if(tempv.size()>=2 && tempv.at(0)==username)
87  {
88  passwdf = true;
89  ifs.close();
90  passwd = tempv.at(1);
91  break;
92  }
93  }
94  ifs.close();
95  }
96  return passwdf;
97 }
98 
99 string FileAuthController::getUserRole(string username)
100 {
101  //bool passwdf = false;
102  ifstream ifs(this->filename.c_str());
103  if(ifs.is_open() && username!="")
104  {
105  string temp;
106  while(getline(ifs, temp))
107  {
108  vector<string> tempv;
109  StringUtil::split(tempv, temp, (this->delimiter));
110  if(tempv.size()>=3 && tempv.at(0)==username)
111  {
112  //passwdf = true;
113  ifs.close();
114  return tempv.at(2);
115  }
116  }
117  ifs.close();
118  }
119  string blnk;
120  return blnk;
121 }
122 
123 string FileAuthController::get(string username, int pos)
124 {
125  //bool passwdf = false;
126  ifstream ifs(this->filename.c_str());
127  if(ifs.is_open() && username!="")
128  {
129  string temp;
130  while(getline(ifs, temp))
131  {
132  vector<string> tempv;
133  StringUtil::split(tempv, temp, (this->delimiter));
134  if((int)tempv.size()>pos && tempv.at(0)==username)
135  {
136  //passwdf = true;
137  ifs.close();
138  return tempv.at(pos);
139  }
140  }
141  ifs.close();
142  }
143  string blnk;
144  return blnk;
145 }