summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/FileUserAccountMapperI.cpp
blob: b57b57af61aca770329e09cdd0a34263ea1f202e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceUtil/DisableWarnings.h>
#include <IceGrid/FileUserAccountMapperI.h>

#include <fstream>

using namespace std;
using namespace IceGrid;

FileUserAccountMapperI::FileUserAccountMapperI(const string& filename)
{
    ifstream file(filename.c_str());
    if(!file)
    {
	throw "cannot open `" + filename + "' for reading: " + strerror(errno);
    }
	    
    const string delim = " \t\r\n";
    while(true)
    {
	string line;
	getline(file, line);
	if(!file)
	{
	    break;
	}

	string::size_type idx = line.find('#');
	if(idx != string::npos)
	{
	    line.erase(idx);
	}
		
	idx = line.find_last_not_of(delim);
	if(idx != string::npos && idx + 1 < line.length())
	{
	    line.erase(idx + 1);
	}
    
	string::size_type beg = line.find_first_not_of(delim);
	if(beg == string::npos)
	{
	    continue;
	}

	string::size_type end = line.find_first_of(delim, beg);
	if(end == string::npos || end <= beg)
	{
	    continue;
	}    
	string account = line.substr(beg, end - beg);

	beg = line.find_first_not_of(delim, end);
	if(beg == string::npos)
	{
	    continue;
	}    
	string user = line.substr(beg);
	
	assert(!user.empty());
	assert(!account.empty());

	_accounts[user] = account;
    }
}

string
FileUserAccountMapperI::getUserAccount(const string& user, const Ice::Current&)
{
    map<string, string>::const_iterator p = _accounts.find(user);
    if(p == _accounts.end())
    {
	throw UserAccountNotFoundException();
    }
    return p->second;
}