]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.cpp
Move authorization from USER to USERS
[stg.git] / projects / stargazer / users_impl.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.61 $
27  $Date: 2010/09/13 05:56:42 $
28  $Author: faust $
29  */
30
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34
35 #include <pthread.h>
36 #include <csignal>
37 #include <cassert>
38 #include <algorithm>
39 #include <utility>
40 #include <string>
41 #include <vector>
42
43 #include "stg/settings.h"
44 #include "stg/common.h"
45 #include "users_impl.h"
46 #include "stg_timer.h"
47
48 using namespace std;
49
50 extern const volatile time_t stgTime;
51
52 //#define USERS_DEBUG 1
53
54 //-----------------------------------------------------------------------------
55 USERS_IMPL::USERS_IMPL(SETTINGS_IMPL * s, STORE * st, TARIFFS * t, const ADMIN * sa)
56     : USERS(),
57       users(),
58       usersToDelete(),
59       /*userIPNotifiersBefore(),
60       userIPNotifiersAfter(),*/
61       ipIndex(),
62       loginIndex(),
63       settings(s),
64       tariffs(t),
65       store(st),
66       sysAdmin(sa),
67       WriteServLog(GetStgLogger()),
68       nonstop(false),
69       isRunning(false),
70       mutex(),
71       thread(),
72       handle(0),
73       searchDescriptors(),
74       onAddNotifiers(),
75       onDelNotifiers(),
76       onAddNotifiersImpl(),
77       onDelNotifiersImpl()
78 {
79 pthread_mutexattr_t attr;
80 pthread_mutexattr_init(&attr);
81 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
82 pthread_mutex_init(&mutex, &attr);
83 }
84 //-----------------------------------------------------------------------------
85 USERS_IMPL::~USERS_IMPL()
86 {
87 pthread_mutex_destroy(&mutex);
88 }
89 //-----------------------------------------------------------------------------
90 int USERS_IMPL::FindByNameNonLock(const string & login, user_iter * user)
91 {
92 map<string, user_iter>::iterator iter;
93 iter = loginIndex.find(login);
94 if (iter != loginIndex.end())
95     {
96     if (user)
97         *user = iter->second;
98     return 0;
99     }
100 return -1;
101 }
102 //-----------------------------------------------------------------------------
103 int USERS_IMPL::FindByName(const string & login, USER_PTR * user)
104 {
105 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
106 user_iter u;
107 int res = FindByNameNonLock(login, &u);
108 if (res)
109     return -1;
110 *user = &(*u);
111 return 0;
112 }
113 //-----------------------------------------------------------------------------
114 bool USERS_IMPL::TariffInUse(const string & tariffName) const
115 {
116 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
117 list<USER_IMPL>::const_iterator iter;
118 iter = users.begin();
119 while (iter != users.end())
120     {
121     if (iter->GetProperty().tariffName.Get() == tariffName)
122         return true;
123     ++iter;
124     }
125 return false;
126 }
127 //-----------------------------------------------------------------------------
128 int USERS_IMPL::Add(const string & login, const ADMIN * admin)
129 {
130 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
131 const PRIV * priv = admin->GetPriv();
132
133 if (!priv->userAddDel)
134     {
135     WriteServLog("%s tried to add user \'%s\'. Access denied.",
136          admin->GetLogStr().c_str(), login.c_str());
137     /*errorStr = "Admin \'" + admin->GetLogin() +
138                "\': tried to add user \'" + ud->login + "\'. Access denied.";*/
139     return -1;
140     }
141
142 //////
143 if (store->AddUser(login))
144     {
145     //TODO
146     //WriteServLog("Admin \'%s\': tried to add user \'%s\'. Access denied.",
147     //     admin->GetLogin().c_str(), ud->login.c_str());
148     return -1;
149     }
150 //////
151
152 USER_IMPL u(settings, store, tariffs, sysAdmin, this);
153
154 /*struct tm * tms;
155 time_t t = stgTime;
156
157 tms = localtime(&t);
158
159 tms->tm_hour = 0;
160 tms->tm_min = 0;
161 tms->tm_sec = 0;
162
163 if (settings->GetDayResetTraff() > tms->tm_mday)
164     tms->tm_mon -= 1;
165
166 tms->tm_mday = settings->GetDayResetTraff();*/
167
168 u.SetLogin(login);
169
170 u.SetPassiveTimeAsNewUser();
171
172 u.WriteConf();
173 u.WriteStat();
174
175 WriteServLog("%s User \'%s\' added.",
176          admin->GetLogStr().c_str(), login.c_str());
177
178 u.OnAdd();
179
180 users.push_front(u);
181
182 AddUserIntoIndexes(users.begin());
183 SetUserNotifiers(users.begin());
184
185     {
186     // Fire all "on add" notifiers
187     set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onAddNotifiers.begin();
188     while (ni != onAddNotifiers.end())
189         {
190         (*ni)->Notify(&users.front());
191         ++ni;
192         }
193     }
194
195     {
196     // Fire all "on add" implementation notifiers
197     set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onAddNotifiersImpl.begin();
198     while (ni != onAddNotifiersImpl.end())
199         {
200         (*ni)->Notify(&users.front());
201         ++ni;
202         }
203     }
204
205 return 0;
206 }
207 //-----------------------------------------------------------------------------
208 void USERS_IMPL::Del(const string & login, const ADMIN * admin)
209 {
210 const PRIV * priv = admin->GetPriv();
211 user_iter u;
212
213 if (!priv->userAddDel)
214     {
215     WriteServLog("%s tried to remove user \'%s\'. Access denied.",
216          admin->GetLogStr().c_str(), login.c_str());
217     return;
218     }
219
220
221     {
222     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
223
224     if (FindByNameNonLock(login, &u))
225         {
226         WriteServLog("%s tried to delete user \'%s\': not found.",
227                      admin->GetLogStr().c_str(),
228                      login.c_str());
229         return;
230         }
231
232     u->SetDeleted();
233     }
234
235     {
236     set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onDelNotifiers.begin();
237     while (ni != onDelNotifiers.end())
238         {
239         (*ni)->Notify(&(*u));
240         ++ni;
241         }
242     }
243
244     {
245     set<NOTIFIER_BASE<USER_IMPL_PTR> *>::iterator ni = onDelNotifiersImpl.begin();
246     while (ni != onDelNotifiersImpl.end())
247         {
248         (*ni)->Notify(&(*u));
249         ++ni;
250         }
251     }
252
253     {
254     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
255
256     u->OnDelete();
257
258     USER_TO_DEL utd;
259     utd.iter = u;
260     utd.delTime = stgTime;
261     usersToDelete.push_back(utd);
262
263     UnSetUserNotifiers(u);
264     DelUserFromIndexes(u);
265
266     WriteServLog("%s User \'%s\' deleted.",
267              admin->GetLogStr().c_str(), login.c_str());
268
269     }
270 }
271 //-----------------------------------------------------------------------------
272 bool USERS_IMPL::Authorize(const std::string & login, uint32_t ip,
273                            uint32_t enabledDirs, const AUTH * auth)
274 {
275 user_iter iter;
276 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
277 if (FindByNameNonLock(login, &iter))
278     {
279     WriteServLog("Attempt to authorize non-existant user '%s'", login.c_str());
280     return false;
281     }
282
283 if (iter->Authorize(ip, enabledDirs, auth))
284     return false;
285
286 if (FindByIPIdx(ip, iter))
287     {
288     if (iter->GetLogin() != login)
289         {
290         WriteServLog("Attempt to authorize user '%s' from ip %s which already occupied by '%s'",
291                      login.c_str(), inet_ntostring(ip).c_str(),
292                      iter->GetLogin().c_str());
293         return false;
294         }
295     return true;
296     }
297
298 AddToIPIdx(iter);
299 return true;
300 }
301 //-----------------------------------------------------------------------------
302 bool USERS_IMPL::Unauthorize(const std::string & login, const AUTH * auth)
303 {
304 user_iter iter;
305 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
306 if (FindByNameNonLock(login, &iter))
307     {
308     WriteServLog("Attempt to unauthorize non-existant user '%s'", login.c_str());
309     return false;
310     }
311
312 uint32_t ip = iter->GetCurrIP();
313
314 iter->Unauthorize(auth);
315
316 if (!iter->GetAuthorized())
317     DelFromIPIdx(ip);
318
319 return true;
320 }
321 //-----------------------------------------------------------------------------
322 int USERS_IMPL::ReadUsers()
323 {
324 vector<string> usersList;
325 usersList.clear();
326 if (store->GetUsersList(&usersList) < 0)
327     {
328     WriteServLog(store->GetStrError().c_str());
329     return -1;
330     }
331
332 user_iter ui;
333
334 for (unsigned int i = 0; i < usersList.size(); i++)
335     {
336     USER_IMPL u(settings, store, tariffs, sysAdmin, this);
337
338     u.SetLogin(usersList[i]);
339     users.push_front(u);
340     ui = users.begin();
341
342     AddUserIntoIndexes(ui);
343     SetUserNotifiers(ui);
344
345     if (ui->ReadConf() < 0)
346         return -1;
347
348     if (ui->ReadStat() < 0)
349         return -1;
350     }
351
352 return 0;
353 }
354 //-----------------------------------------------------------------------------
355 void * USERS_IMPL::Run(void * d)
356 {
357 printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
358 USERS_IMPL * us = (USERS_IMPL*) d;
359
360 struct tm t;
361 time_t tt = stgTime;
362 localtime_r(&tt, &t);
363
364 int min = t.tm_min;
365 int day = t.tm_mday;
366
367 printfd(__FILE__,"Day = %d Min = %d\n", day, min);
368
369 time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC;
370 string monFile = us->settings->GetMonitorDir() + "/users_r";
371 printfd(__FILE__, "Monitor=%d file USERS %s\n", us->settings->GetMonitoring(), monFile.c_str());
372
373 us->isRunning = true;
374 while (us->nonstop)
375     {
376     //printfd(__FILE__,"New Minute. old = %02d current = %02d\n", min, t->tm_min);
377     //printfd(__FILE__,"New Day.    old = %2d current = %2d\n", day, t->tm_mday);
378
379     for_each(us->users.begin(), us->users.end(), mem_fun_ref(&USER_IMPL::Run));
380
381     tt = stgTime;
382     localtime_r(&tt, &t);
383
384     if (min != t.tm_min)
385         {
386         printfd(__FILE__,"Sec = %d\n", stgTime);
387         printfd(__FILE__,"New Minute. old = %d current = %d\n", min, t.tm_min);
388         min = t.tm_min;
389
390         us->NewMinute(t);
391         }
392
393     if (day != t.tm_mday)
394         {
395         printfd(__FILE__,"Sec = %d\n", stgTime);
396         printfd(__FILE__,"New Day. old = %d current = %d\n", day, t.tm_mday);
397         day = t.tm_mday;
398         us->NewDay(t);
399         }
400
401     if (us->settings->GetMonitoring() && (touchTime + MONITOR_TIME_DELAY_SEC <= stgTime))
402         {
403         //printfd(__FILE__, "Monitor=%d file TRAFFCOUNTER %s\n", tc->monitoring, monFile.c_str());
404         touchTime = stgTime;
405         TouchFile(monFile.c_str());
406         }
407
408     stgUsleep(100000);
409     } //while (us->nonstop)
410
411 user_iter ui = us->users.begin();
412 while (ui != us->users.end())
413     {
414     us->UnSetUserNotifiers(ui);
415     us->DelUserFromIndexes(ui);
416     ++ui;
417     }
418
419 list<USER_TO_DEL>::iterator iter;
420 iter = us->usersToDelete.begin();
421 while (iter != us->usersToDelete.end())
422     {
423     iter->delTime -= 2 * userDeleteDelayTime;
424     ++iter;
425     }
426 us->RealDelUser();
427
428 us->isRunning = false;
429
430 return NULL;
431 }
432 //-----------------------------------------------------------------------------
433 void USERS_IMPL::NewMinute(const struct tm & t)
434 {
435 //Write traff, reset session traff. Fake disconnect-connect
436 if (t.tm_hour == 23 && t.tm_min == 59)
437     {
438     printfd(__FILE__,"MidnightResetSessionStat\n");
439     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::MidnightResetSessionStat));
440     }
441
442 if (TimeToWriteDetailStat(t))
443     {
444     //printfd(__FILE__, "USER::WriteInetStat\n");
445     int usersCnt = 0;
446
447     // ðÉÛÅÍ ÀÚÅÒÏ× ÞÁÓÔÑÍÉ. ÷ ÐÅÒÅÒÙ×ÁÈ ×ÙÚÙ×ÁÅÍ USER::Run
448     list<USER_IMPL>::iterator usr = users.begin();
449     while (usr != users.end())
450         {
451         usersCnt++;
452         usr->WriteDetailStat();
453         ++usr;
454         if (usersCnt % 10 == 0)
455             for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
456         }
457     }
458
459 RealDelUser();
460 }
461 //-----------------------------------------------------------------------------
462 void USERS_IMPL::NewDay(const struct tm & t)
463 {
464 struct tm t1;
465 time_t tt = stgTime;
466 localtime_r(&tt, &t1);
467 int dayFee = settings->GetDayFee();
468
469 if (dayFee == 0)
470     dayFee = DaysInCurrentMonth();
471
472 printfd(__FILE__, "DayFee = %d\n", dayFee);
473 printfd(__FILE__, "Today = %d DayResetTraff = %d\n", t1.tm_mday, settings->GetDayResetTraff());
474 printfd(__FILE__, "DayFeeIsLastDay = %d\n", settings->GetDayFeeIsLastDay());
475
476 if (!settings->GetDayFeeIsLastDay())
477     {
478     printfd(__FILE__, "DayResetTraff - 1 -\n");
479     DayResetTraff(t1);
480     //printfd(__FILE__, "DayResetTraff - 1 - 1 -\n");
481     }
482
483 if (settings->GetSpreadFee())
484     {
485     printfd(__FILE__, "Spread DayFee\n");
486     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFeeSpread));
487     }
488 else
489     {
490     if (t.tm_mday == dayFee)
491         {
492         printfd(__FILE__, "DayFee\n");
493         for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFee));
494         }
495     }
496
497 if (settings->GetDayFeeIsLastDay())
498     {
499     printfd(__FILE__, "DayResetTraff - 2 -\n");
500     DayResetTraff(t1);
501     }
502 }
503 //-----------------------------------------------------------------------------
504 void USERS_IMPL::DayResetTraff(const struct tm & t1)
505 {
506 int dayResetTraff = settings->GetDayResetTraff();
507 if (dayResetTraff == 0)
508     dayResetTraff = DaysInCurrentMonth();
509 if (t1.tm_mday == dayResetTraff)
510     {
511     printfd(__FILE__, "ResetTraff\n");
512     for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessNewMonth));
513     //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff));
514     }
515 }
516 //-----------------------------------------------------------------------------
517 int USERS_IMPL::Start()
518 {
519 if (ReadUsers())
520     {
521     WriteServLog("USERS: Error: Cannot read users!");
522     return -1;
523     }
524
525 nonstop = true;
526 if (pthread_create(&thread, NULL, Run, this))
527     {
528     WriteServLog("USERS: Error: Cannot start thread!");
529     return -1;
530     }
531 return 0;
532 }
533 //-----------------------------------------------------------------------------
534 int USERS_IMPL::Stop()
535 {
536 printfd(__FILE__, "USERS::Stop()\n");
537
538 if (!isRunning)
539     {
540     //printfd(__FILE__, "Alredy stopped\n");
541     return 0;
542     }
543
544 nonstop = false;
545
546 //5 seconds to thread stops itself
547 struct timespec ts = {0, 200000000};
548 for (size_t i = 0; i < 25 * (users.size() / 50 + 1); i++)
549     {
550     if (!isRunning)
551         break;
552
553     nanosleep(&ts, NULL);
554     }
555
556 //after 5 seconds waiting thread still running. now kill it
557 if (isRunning)
558     {
559     printfd(__FILE__, "kill USERS thread.\n");
560     //TODO pthread_cancel()
561     if (pthread_kill(thread, SIGINT))
562         {
563         //errorStr = "Cannot kill USERS thread.";
564         //printfd(__FILE__, "Cannot kill USERS thread.\n");
565         //return 0;
566         }
567     printfd(__FILE__, "USERS killed\n");
568     }
569
570 printfd(__FILE__, "Before USERS::Run()\n");
571 for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
572
573 // 'cause bind2st accepts only constant first param
574 for (list<USER_IMPL>::iterator it = users.begin();
575      it != users.end();
576      ++it)
577     it->WriteDetailStat(true);
578
579 for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteStat));
580 //for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf));
581
582 printfd(__FILE__, "USERS::Stop()\n");
583 return 0;
584 }
585 //-----------------------------------------------------------------------------
586 void USERS_IMPL::RealDelUser()
587 {
588 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
589
590 printfd(__FILE__, "RealDelUser() users to del: %d\n", usersToDelete.size());
591
592 list<USER_TO_DEL>::iterator iter;
593 iter = usersToDelete.begin();
594 while (iter != usersToDelete.end())
595     {
596     printfd(__FILE__, "RealDelUser() user=%s\n", iter->iter->GetLogin().c_str());
597     if (iter->delTime + userDeleteDelayTime < stgTime)
598         {
599         printfd(__FILE__, "RealDelUser() user=%s removed from DB\n", iter->iter->GetLogin().c_str());
600         if (store->DelUser(iter->iter->GetLogin()))
601             {
602             WriteServLog("Error removing user \'%s\' from database.", iter->iter->GetLogin().c_str());
603             }
604         users.erase(iter->iter);
605         usersToDelete.erase(iter++);
606         }
607     else
608         {
609         ++iter;
610         }
611     }
612 return;
613 }
614 //-----------------------------------------------------------------------------
615 void USERS_IMPL::AddToIPIdx(user_iter user)
616 {
617 printfd(__FILE__, "USERS: Add IP Idx\n");
618 uint32_t ip = user->GetCurrIP();
619 //assert(ip && "User has non-null ip");
620 if (!ip)
621     return; // User has disconnected
622
623 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
624
625 const map<uint32_t, user_iter>::iterator it(
626         ipIndex.lower_bound(ip)
627 );
628
629 assert((it == ipIndex.end() || it->first != ip) && "User is not in index");
630
631 ipIndex.insert(it, std::make_pair(ip, user));
632 }
633 //-----------------------------------------------------------------------------
634 void USERS_IMPL::DelFromIPIdx(uint32_t ip)
635 {
636 printfd(__FILE__, "USERS: Del IP Idx\n");
637 assert(ip && "User has non-null ip");
638
639 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
640
641 const map<uint32_t, user_iter>::iterator it(
642         ipIndex.find(ip)
643 );
644
645 if (it == ipIndex.end())
646     return;
647
648 ipIndex.erase(it);
649 }
650 //-----------------------------------------------------------------------------
651 bool USERS_IMPL::FindByIPIdx(uint32_t ip, user_iter & iter) const
652 {
653 map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
654 if (it == ipIndex.end())
655     return false;
656 iter = it->second;
657 return true;
658 }
659 //-----------------------------------------------------------------------------
660 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_PTR * usr) const
661 {
662 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
663
664 user_iter iter;
665 if (FindByIPIdx(ip, iter))
666     {
667     *usr = &(*iter);
668     return 0;
669     }
670
671 return -1;
672 }
673 //-----------------------------------------------------------------------------
674 int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_IMPL ** usr) const
675 {
676 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
677
678 user_iter iter;
679 if (FindByIPIdx(ip, iter))
680     {
681     *usr = &(*iter);
682     return 0;
683     }
684
685 return -1;
686 }
687 //-----------------------------------------------------------------------------
688 bool USERS_IMPL::IsIPInIndex(uint32_t ip) const
689 {
690 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
691
692 map<uint32_t, user_iter>::const_iterator it(ipIndex.find(ip));
693
694 return it != ipIndex.end();
695 }
696 //-----------------------------------------------------------------------------
697 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
698 {
699 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
700 onAddNotifiers.insert(n);
701 }
702 //-----------------------------------------------------------------------------
703 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
704 {
705 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
706 onAddNotifiers.erase(n);
707 }
708 //-----------------------------------------------------------------------------
709 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
710 {
711 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
712 onDelNotifiers.insert(n);
713 }
714 //-----------------------------------------------------------------------------
715 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
716 {
717 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
718 onDelNotifiers.erase(n);
719 }
720 //-----------------------------------------------------------------------------
721 void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
722 {
723 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
724 onAddNotifiersImpl.insert(n);
725 }
726 //-----------------------------------------------------------------------------
727 void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> * n)
728 {
729 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
730 onAddNotifiersImpl.erase(n);
731 }
732 //-----------------------------------------------------------------------------
733 void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
734 {
735 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
736 onDelNotifiersImpl.insert(n);
737 }
738 //-----------------------------------------------------------------------------
739 void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> * n)
740 {
741 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
742 onDelNotifiersImpl.erase(n);
743 }
744 //-----------------------------------------------------------------------------
745 int USERS_IMPL::OpenSearch()
746 {
747 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
748 handle++;
749 searchDescriptors[handle] = users.begin();
750 return handle;
751 }
752 //-----------------------------------------------------------------------------
753 int USERS_IMPL::SearchNext(int h, USER_PTR * user)
754 {
755     USER_IMPL * ptr = NULL;
756     if (SearchNext(h, &ptr))
757         return -1;
758     *user = ptr;
759     return 0;
760 }
761 //-----------------------------------------------------------------------------
762 int USERS_IMPL::SearchNext(int h, USER_IMPL ** user)
763 {
764 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
765
766 if (searchDescriptors.find(h) == searchDescriptors.end())
767     {
768     WriteServLog("USERS. Incorrect search handle.");
769     return -1;
770     }
771
772 if (searchDescriptors[h] == users.end())
773     return -1;
774
775 while (searchDescriptors[h]->GetDeleted())
776     {
777     ++searchDescriptors[h];
778     if (searchDescriptors[h] == users.end())
779         {
780         return -1;
781         }
782     }
783
784 *user = &(*searchDescriptors[h]);
785
786 ++searchDescriptors[h];
787
788 return 0;
789 }
790 //-----------------------------------------------------------------------------
791 int USERS_IMPL::CloseSearch(int h)
792 {
793 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
794 if (searchDescriptors.find(h) != searchDescriptors.end())
795     {
796     searchDescriptors.erase(searchDescriptors.find(h));
797     return 0;
798     }
799
800 WriteServLog("USERS. Incorrect search handle.");
801 return -1;
802 }
803 //-----------------------------------------------------------------------------
804 void USERS_IMPL::SetUserNotifiers(user_iter user)
805 {
806 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
807
808 /*PROPERTY_NOTIFER_IP_BEFORE nb(*this, user);
809 PROPERTY_NOTIFER_IP_AFTER  na(*this, user);
810
811 userIPNotifiersBefore.push_front(nb);
812 userIPNotifiersAfter.push_front(na);
813
814 user->AddCurrIPBeforeNotifier(&(*userIPNotifiersBefore.begin()));
815 user->AddCurrIPAfterNotifier(&(*userIPNotifiersAfter.begin()));*/
816 }
817 //-----------------------------------------------------------------------------
818 void USERS_IMPL::UnSetUserNotifiers(user_iter user)
819 {
820 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
821
822 /*list<PROPERTY_NOTIFER_IP_BEFORE>::iterator  bi;
823 list<PROPERTY_NOTIFER_IP_AFTER>::iterator   ai;
824
825 bi = userIPNotifiersBefore.begin();
826 while (bi != userIPNotifiersBefore.end())
827     {
828     if (bi->GetUser() == user)
829         {
830         bi->GetUser()->DelCurrIPBeforeNotifier(&(*bi));
831         userIPNotifiersBefore.erase(bi);
832         //printfd(__FILE__, "Notifier Before removed. User %s\n", bi->GetUser()->GetLogin().c_str());
833         break;
834         }
835     ++bi;
836     }
837
838 ai = userIPNotifiersAfter.begin();
839 while (ai != userIPNotifiersAfter.end())
840     {
841     if (ai->GetUser() == user)
842         {
843         ai->GetUser()->DelCurrIPAfterNotifier(&(*ai));
844         userIPNotifiersAfter.erase(ai);
845         //printfd(__FILE__, "Notifier After removed. User %s\n", ai->GetUser()->GetLogin().c_str());
846         break;
847         }
848     ++ai;
849     }*/
850 }
851 //-----------------------------------------------------------------------------
852 void USERS_IMPL::AddUserIntoIndexes(user_iter user)
853 {
854 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
855 loginIndex.insert(make_pair(user->GetLogin(), user));
856 }
857 //-----------------------------------------------------------------------------
858 void USERS_IMPL::DelUserFromIndexes(user_iter user)
859 {
860 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
861 loginIndex.erase(user->GetLogin());
862 }
863 //-----------------------------------------------------------------------------
864 bool USERS_IMPL::TimeToWriteDetailStat(const struct tm & t)
865 {
866 int statTime = settings->GetDetailStatWritePeriod();
867
868 switch (statTime)
869     {
870     case dsPeriod_1:
871         if (t.tm_min == 0)
872             return true;
873         break;
874     case dsPeriod_1_2:
875         if (t.tm_min % 30 == 0)
876             return true;
877         break;
878     case dsPeriod_1_4:
879         if (t.tm_min % 15 == 0)
880             return true;
881         break;
882     case dsPeriod_1_6:
883         if (t.tm_min % 10 == 0)
884             return true;
885         break;
886     }
887 return false;
888 }