Skip to content

USERS Module

Jurek Muszyński edited this page Jun 18, 2019 · 69 revisions

Database

Install MySQL

Linux

sudo yum install mysql
sudo yum install mysql-devel

In case of trouble see MySQL documentation.

Windows

Get the installation package here. You can ignore v8 ads, as v5.7 already had everything that was ever needed for this kind of project and works seamlessly with old plain, fast and straightforward C connector. The last version was 5.7.26. Choose Custom installation and tick only MySQL server and Connector/C 6.1. Unless of course, you know what you're doing and want some additives.

SQL script

Create database

Let's suppose we want to create a database for Toy Facebook.

  1. You need to have the path to MySQL bin directory added to your environment. In the command line you need to be able to invoke mysql command.

  2. Log in to mysql command line tool:

mysql -u root --password=mysqlrootpassword
  1. Create user:
create user 'silgy'@'localhost' identified by 'silgypassword';
grant all privileges on * . * to 'silgy'@'localhost';
quit
  1. Go to mysql again, as silgy user:
mysql -u silgy --password=silgypassword
  1. Create database:
create database tfb;
quit
  1. Create tables. Use this SQL script.
cd tfb/lib
mysql -u silgy --password=silgypassword tfb < users.sql

silgy_app.h

Both DBMYSQL and USERS must be defined:

#define DBMYSQL
#define USERS

Compilation script

Add MySQL library path and name, so your m script would look like this:

#!/bin/sh

echo Making silgy_app...

g++ silgy_app.cpp \
../lib/silgy_eng.c ../lib/silgy_lib.c ../lib/silgy_usr.c \
-I. -I../lib \
-I/usr/include/mysql55 \
-L/usr/lib64/mysql -lmysqlclient \
-lrt -lz \
-s -O3 \
-o ../bin/silgy_app

At least this script works on AWS AMI distro, you may need to verify your paths if using something else.

Configuration

Add database connection details to $SILGYDIR/bin/silgy.conf:

dbName=tfb
dbUser=silgy
dbPassword=silgypassword

Sample code

You can download Toy Facebook project as a template for a typical web application with users.

Logged in user sessions in Silgy

You may want to learn about sessions in Silgy first.

By default, users are authenticated by login. login form field can contain login or email and silgy_usr_login() will try both. Alternatively, you can use USERSBYEMAIL switch to use exclusively email.

Successful silgy_usr_login() call does the following:

  • Sets the uses array record values (accessible via US macro),
  • Adds record to users_logins table,
  • Adds ls cookie to the response,
  • Updates users.visits and users.last_login.

Keep Me Logged In

Adding keep=on to the login request will set ls cookie expiration time to now + USER_KEEP_LOGGED_DAYS days. Therefore – until you call silgy_usr_logout() – every subsequent request with valid ls cookie and the same User Agent as in the initial request, will automatically mark session as logged in.

Otherwise

Without keep=on cookie does not have expiration time, so by default it will expire by the end of the current browser session.

Caching

Logged in sessions are cached for LUSES_TIMEOUT seconds after last activity.

Unsuccessful logins

Unsuccessful login count and time is stored in users.ula_cnt and users.ula_time. Then there are 4 tresholds affecting the next attempts:

Macro Description Default
MAX_ULA_BEFORE_FIRST_SLOW Maximum unsuccessful login attempts before first slowing down (1 attempt per minute will be allowed) 10
MAX_ULA_BEFORE_SECOND_SLOW Maximum unsuccessful login attempts before second slowing down (1 attempt per hour will be allowed) 25
MAX_ULA_BEFORE_THIRD_SLOW Maximum unsuccessful login attempts before third slowing down (1 attempt per 23 hours will be allowed) 100
MAX_ULA_BEFORE_LOCK Maximum unsuccessful login attempts before locking user out 1000
Clone this wiki locally