Skip to content

Commit

Permalink
add roles
Browse files Browse the repository at this point in the history
  • Loading branch information
iljavaleev committed Jul 18, 2024
1 parent 5e90a9e commit 4690c84
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion botstaff/include/botstaff/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ std::vector<int> get_curent_ymd();


bool is_admin(long);
bool is_teacher(const BotUser&);
bool is_teacher(const std::shared_ptr<BotUser>&);
bool is_teacher(long chat_id);
std::unordered_set<int> get_lesson_days(int, int, long, const std::string&);

Expand Down
19 changes: 16 additions & 3 deletions botstaff/src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
#include <sstream>
#include <chrono>
#include <memory>
#include <unordered_map>
#include <boost/locale/encoding_utf.hpp>


#include "botstaff/database/PSQL.hpp"
#include "botstaff/database/CRUD.hpp"
#include "botstaff/handlers/Handlers.hpp"

using namespace TgBot;


static std::shared_ptr<std::unordered_map<long, std::string>> roles;

int dayNumber(int year, int month, int day)
{
static std::vector<int> t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
Expand Down Expand Up @@ -120,10 +125,10 @@ bool is_admin(long chat_id)
return std::stol(s_chat_id) == chat_id;
}

bool is_teacher(const BotUser& user)
bool is_teacher(const std::shared_ptr<BotUser>& user)
{
return is_admin(user.chat_id) || (
!user.empty() && user.is_active && (user.role == "teacher"
return is_admin(user->chat_id) || (
!user->empty() && user->is_active && (user->role == "teacher"
)
);
}
Expand All @@ -133,7 +138,15 @@ bool is_teacher(long chat_id)
if (is_admin(chat_id))
return true;

if (roles->size() == 100)
roles->clear();

if (roles->contains(chat_id))
return roles->at(chat_id) == "teacher";

std::shared_ptr<BotUser> user = BotUser::get(chat_id);
if (user->is_active)
roles->insert({chat_id, user->role});
return !user->empty() && user->is_active && (user->role == "teacher");
}

Expand Down
5 changes: 3 additions & 2 deletions botstaff/src/handlers/Handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace CommandHandlers
nullptr,
teacherKeyboards::create_teacher_start_kb(true)
);
else if(is_teacher(*user))
else if(is_teacher(user))
return bot.getApi().sendMessage(
chat_id,
"Вход для учителя",
Expand Down Expand Up @@ -101,7 +101,8 @@ namespace Handlers

return bot.getApi().sendMessage(
user_chat_id,
"Your message is: " + message->text
"Сообщение " + message->text + " не является командой "
"и вне контекста"
);
}
Message::Ptr calendar_handler::operator()(const CallbackQuery::Ptr& query)
Expand Down

0 comments on commit 4690c84

Please sign in to comment.