From e1b7830ab470deb75d8e85e83417ec02e9faba8b Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Mon, 8 Jan 2018 19:37:09 +0100 Subject: [PATCH] checking for username being at least 5 characters --- src/identity.cc | 16 ++++++++++++++-- src/identity.hh | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/identity.cc b/src/identity.cc index 796738b..95d2fa9 100644 --- a/src/identity.cc +++ b/src/identity.cc @@ -13,11 +13,15 @@ namespace pEp { Identity::Identity(string address, string username, string user_id, string fpr, int comm_type, string lang, identity_flags_t flags) - : _ident(new_identity(address.c_str(), fpr.c_str(), - user_id.c_str(), username.c_str()), &::free_identity) + : _ident(new_identity(address.c_str(), fpr.c_str(), username.c_str(), + user_id.c_str()), &::free_identity) { if (!_ident) throw bad_alloc(); + if (username.length() && username.length() < 5) { + _ident = nullptr; + throw length_error("username must be at least 5 characters"); + } _ident->comm_type = (PEP_comm_type) comm_type; _ident->flags = (identity_flags_t) flags; this->lang(lang); @@ -85,6 +89,14 @@ namespace pEp { return string(_ident->username) + " <" + _ident->address + ">"; } + void Identity::username(string value) + { + if (value.length() && value.length() < 5) + throw length_error("username must be at least 5 characters"); + + str_attr(_ident->username, value); + } + void Identity::lang(string value) { if (value == "") diff --git a/src/identity.hh b/src/identity.hh index a607721..9a792b8 100644 --- a/src/identity.hh +++ b/src/identity.hh @@ -41,7 +41,7 @@ namespace pEp { void user_id(string value) { str_attr(_ident->user_id, value); } string username() { return str_attr(_ident->username); } - void username(string value) { str_attr(_ident->username, value); } + void username(string value); PEP_comm_type comm_type() { return _ident->comm_type; } void comm_type(PEP_comm_type value) { _ident->comm_type = value; };