git.fiddlerwoaroof.com
installer.cc
7d075ab4
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
  * terms of the  GNU General Public License  version 2  or
  * later.
  *
  * You should have received a copy of the license with this
  * file. If not, you will find a copy in the "LICENSE" file
  * at https://github.com/cjdev/dual-control.
  */
 
bf756dea
 #include <string>
 #include <vector>
 
 #include "installer.h"
 #include "token.h"
 #include "sys_unistd.h"
 #include "user.h"
f2a91ef8
 #include "generator.h"
bf756dea
 
7d075ab4
 namespace
 {
 
 class impl : public installer_ifc
 {
 private:
     tokens tokens_;
     unistd unistd_;
     directory directory_;
     generator generator_;
 public:
     impl (const tokens &tokens, const unistd &unistd,
f2a91ef8
           const directory &directory, const generator &generator) :
7d075ab4
         tokens_ (tokens), unistd_ (unistd), directory_ (directory),
         generator_ (generator) {}
     std::string install_token() const override
     {
         const char *c_user_name = unistd_.getlogin();
 
         if (c_user_name == nullptr) {
             return "";
bf756dea
         }
7d075ab4
 
         std::string user_name = c_user_name;
 
         auto found_user = directory_.find_user (user_name);
 
         if (found_user.empty()) {
             return "";
         }
 
         user user (found_user[0]);
         std::string token (generator_());
         tokens_.save (user, token);
         return token;
     }
bf756dea
 };
 
 }
 
7d075ab4
 installer installer::create (const tokens &tokens, const unistd &unistd,
f2a91ef8
                              const directory &directory, const generator &generator)
7d075ab4
 {
     return installer (std::make_shared<impl> (tokens, unistd, directory,
                       generator));
bf756dea
 }