I've got a program that asks users for username and password, this is how I've defined it :
system("cls");
cout << "Enter Your Usename Please:" << endl;
cin >> staff_user;
cout << "Enter Your Password Please:" << endl;
cin >> staff_password;
if (staff_user == "staff" && staff_password == "generic"){
std.staff_menu();
}
else { cout << "Sorry, Information Entered Is Invalid" << endl; }
system("pause");
system("cls");
main();
so once the right username and password is entered, user get to see the other menu, but the thing is, i'm looking for a way to change this later on, I mean I want to allow that person to change his/her password. What are the possible ways to do such a thing ?!
If you only want the password to be remembered for this execution of the program then you can have it as part of a class and this class be initialized at the beginning of main and then pass it as a pointer to the various parts of your program.
If you want it to exist between different executions of your program then you need to save it - the simplest way is simply writing to a file but if your program is going to get large (or have a lot of users) then the best way is the use of a database.
When saving a username/password combination to a text file I tend to write them to the same line with a deliminiter in between that will never exist in either username or password. Then when you try to log a user in or change their password you can loop through the file line by line until you find the right username.
Keep in mind though that in real life you should NEVER store a plaintext password.
Related
I am trying to create a Form with apps script and for some reason it will not allow the simplest phone number validation in a TextItem input field. I only need the user to enter their 10 digit number with no spaces, dashes, dots, with area code included. I know this isn't the technical "best" way to validate phone numbers but for our purposes it works. This is section of code I have to generate the field. (edited to make a more reproducible example)
function CreateForm() {
var form = FormApp.create('Test');
var tenantNum = form.addTextItem()
.setTitle("Tenant Phone #");
var phoneValid = FormApp.createTextValidation()
.setHelpText("Please enter valid phone number, 10-digits, no symbols or spaces.")
.requireTextMatchesPattern(/\d{10}/g)
.build();
tenantNum.setValidation(phoneValid);
console.log(form.getPublishedUrl())
}
I have also tried other things like:
(No capture group or global tag, both individually)
.requireTextMatchesPattern(/\d{10}/)
Entering the regex as a string literal.
.requireTextMatchesPattern("\d{10}")
Even dumdum stuff like.
.requireTextMatchesPattern(/[0-9]{10}/)
or
.requireTextMatchesPattern(/\d\d\d\d\d\d\d\d\d\d/)
Just to see if it works. I have also tried .requireTextContainsPattern() as well.
Whenever you enter in the form however i get this response:
Please enter valid phone number, 10-digits, no symbols or spaces.
My only thought is that there might be some confusion with whether or not the user is entering a string vs. number and this method only works on strings. But this validation
and regex work fine when you enter it into the form creation manually so I'm completely lost. Any help is appreciated!
Just a guess... Try this:
.requireTextMatchesPattern("\\d{10}")
Update
It works fine for me:
function CreateForm() {
var form = FormApp.create('Test');
var tenantNum = form.addTextItem()
.setTitle("Tenant Phone #");
var phoneValid = FormApp.createTextValidation()
.setHelpText("Please enter valid phone number, 10-digits, no symbols or spaces.")
.requireTextMatchesPattern("\\d{10}")
.build();
tenantNum.setValidation(phoneValid);
console.log(form.getPublishedUrl())
}
And the "not existed" method setHelpText() works fine as well, as you can tell from the screenshots.
The gets function in crystal is not waiting for user input. When I start my console application it immediately outputs an error like the one below. It is saying that the second parameter given to the in_array function is Nil but the program doesn't even ask for user input.
My code looks like below.
# Only alice and bob are greeted.
def in_array(array : Array, contains : String)
array.each { |e|
if e == contains
return true;
end
}
return false;
end
allowed = ["Alice", "Bob"]
puts "Please enter your name to gain access."
name = gets
isAllowed = in_array(allowed, name)
if isAllowed
puts "You can enter the secret room"
else
puts "You are not allowed to enter the secret room."
end
The new version of my code using includes? and read_line
# Only alice and bob are greeted.
allowed = ["Alice", "Bob"]
puts "Please enter your name to gain access."
name = read_line.chomp
if allowed.includes?(name)
puts "You can enter the secret room"
else
puts "You are not allowed to enter the secret room."
end
But when I enter Bob into the name variable the includes? method returns false and executes the else statement.
A few things:
The error you are seeing is a compile error. This means that your program is not running, it failed to compile.
gets can return nil (as indicated in the docs), for example if the user presses Ctrl+C, so you have to handle this. You can either do if name = gets, use gets.not_nil! if you don't care about this case, or use read_line which is equivalent to gets.not_nil!
Array has a method includes? that does what you are trying to implement
I understand how to read a string from STDIN (noted below), but my problem is that the characters are displayed on the screen. How can I make the string hidden like the Unix/Linux password prompts?
print "Password: "
pass = gets.as(String).strip
The standard library currently provides no way for this. A quick workaround is to bind getpass(3):
lib LibC
fun getpass(prompt : Char*) : Char*
end
def getpass(prompt : String)
password = LibC.getpass(prompt)
raise Errno.new("getpass") unless password
String.new(password)
end
password = getpass("Enter password: ")
However note that this function is deprecated by glibc and the termios(3) interface should be used. I opened a pull request for this, so hopefully in Crystal version 0.19.0 or later you'll be able to:
print "Enter password: "
password = STDIN.noecho &.gets.try &.chomp
puts
I am new to C++ and Qt, but I have been playing around with it for a couple of days and I need to come up with a basic prototype of my product by Friday, so there is not much time to convert my 7 years of PHP knowledge into C++ knowledge, as I am sure that it takes a lifetime to master C++. I am getting stuck from time to time in the last couple of days due to non-existing knowledge about the small bits and bytes. At this time I have even no idea what to look for on the Internet.
First of all, I am using Qt as my framework to do a simple GUI network client that can talk to my PHP application. I wanted to create a very simple WebAPI class in this application and have the class "webapi". The scenario is very simple, the user starts the application and the applications checks if the user is logged in or not. If not, then it opens up a login dialog. After entering the information (username / password) into the dialog the user object is filled and the method "authenticate" is called.
The authenticate method then calls the fUser method in the webapi class to make a request to the server holding some information to authenticate the user against the server's database.
In code it looks like this:
Savor.cpp:
user = new User();
while ( user->isLoggedIn() != true )
{
LoginDialog loginWindow;
loginWindow.setModal(true);
int result = loginWindow.exec();
if ( result == QDialog::Accepted )
{
user->authenticate(loginWindow.getUsername(), loginWindow.getPassword());
if ( !user->isLoggedIn() )
{
loginWindow.setUsername(loginWindow.getUsername());
loginWindow.exec();
}
}
else
{
exit(1);//exit with code 1
}
}
User.cpp:
void User::authenticate(QString username, QString password)
{
qDebug() << username;
qDebug() << password;
if ( username != "" && password != "")
{
webapi wapi;
loggedIn = wapi.fUser(this);
}
else
{
loggedIn = false;
}
}
webapi.cpp:
/**
Represents the fUser method on the server,
it wants to get a user object
the user will need to be authenticated with this
then all infos about user are transfered (RSA Keypair etc)
* #brief webapi::fUser
* #param username
* #param password
* #return User
*/
bool webapi::fUser(User baseUser)
{
return true;
}
Now you can clearly see that I am not doing anything at the moment in the webapi::fUser method. In fact, I am not even returning what I would like to return. Instead of a boolean I would like to return a User object, actually the same that I got in the first place through the parameter. However, i get a copy constructor error when I do that. (In my savor.h file I have declared a private attribute as a pointer => User *user;)
So the question is, what am I doing wrong when I call the fUser method? Why can I not simply pass the user object itself to the method ? I have not got around to fully understand const, and pointers and when to use what.
With Qt creator I actually use the MS Visual C++ compiler which throws the error as in the title:
C2664 'webapi::fUser' : cannot convert paramter 1 from 'User *const' to 'User'
I have read http://msdn.microsoft.com/en-us/library/s5b150wd(v=vs.71).aspx this page explaining when this happens, the only solutions is the conversion of the object itself.
If thats the case I might approach the entire problem in the wrong way.
I am looking forward to your tips and help on this matter.
Thank you very much.
webapi::fuser takes a User by value, but you are passing it a User* here:
wapi.fUser(this);
Either pass a User:
wapi.fUser(*this);
or change webapi to take a pointer to User.
my question is: It there any way that in renderEditor.template know what user is logged in and what roles does he have?
What I want to do: In my app i want some field change when logged user is admin ("ROLE_ADMIN") - normal user will have is html select tag only 1 option, by default selected, and when admin user will be logged in, list of all users appear.
I'm using Spring Security Plugin + Grails 2.1.1.
What I have already tried:
Add Spring Security Service (def springSecurityService) but it was always null
Try to pass boolean argument - no effect
Any help will be appreciated!
EDIT Many thanks for your answers. However, my question wasn't precised enough, so I include some pseudocode that maybe explain better what I want achieve (method from renderTemplate.template)
private renderManyToOne(domainClass,property) {
if (property.association) {
def sb = new StringBuilder()
sb << '<g:select'
...
if (/*loged user is admin*/) {
sb << ' from="${' << property.type.name << '.list()}"'
}else{
sb << ' from="${user}"'/*only loged user can be selected*/
}
...
sb << '/>'
sb as String
}
}
There's a taglib with spring security
http://grails-plugins.github.com/grails-spring-security-core/docs/manual/guide/6%20Helper%20Classes.html#6.1%20SecurityTagLib
In my app I use it like this:
<ul>
<sec:access expression="hasRole('ROLE_ADMIN')">
<li>Add New Feed</li>
</sec:access>
</ul>
The renderEditor.template is not rendered with the usual GSP template engine. It's rendered with a standard SimpleTemplateEngine with just few simple bindings. However, the output of this template then gets rendered with the GSP template engine. It's fairly confusing, but you can have renderEditor.template output GSP code. For example:
<% if (property.type == Boolean || property.type == boolean)
out << renderBooleanEditor(domainClass, property)
else ...
else if (property.type == String && domainInstance == 'specialField') {
out << '''<g:if test="${org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils.ifAllGranted('ROLE_ADMIN')}">'''
out << renderStringEditor(domainClass, property)
out << '''</g:if>'''
out << '''<g:else>'''
out << renderStringSelectEditor(domainClass, property)
out << '''</g:else>'''
}
...