Skip to content

Commit

Permalink
Merge pull request #3989 from nextcloud/askForPassword
Browse files Browse the repository at this point in the history
Public share: if ask for password is set, show an optional password dialog
  • Loading branch information
AndyScherzinger authored May 14, 2019
2 parents 3b24d09 + 1edf2cd commit b4f36f4
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 50 deletions.
2 changes: 1 addition & 1 deletion scripts/analysis/findbugs-results.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
438
438
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,8 @@ public OCCapability saveCapabilities(OCCapability capability) {
capability.getFilesSharingPublicEnabled().getValue());
cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED,
capability.getFilesSharingPublicPasswordEnforced().getValue());
cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD,
capability.getFilesSharingPublicAskForOptionalPassword().getValue());
cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED,
capability.getFilesSharingPublicExpireDateEnabled().getValue());
cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS,
Expand Down Expand Up @@ -2078,6 +2080,9 @@ private OCCapability createCapabilityInstance(Cursor c) {
.getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED))));
capability.setFilesSharingPublicPasswordEnforced(CapabilityBooleanType.fromValue(c.getInt(c
.getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED))));
capability.setFilesSharingPublicAskForOptionalPassword(
CapabilityBooleanType.fromValue(
c.getInt(c.getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD))));
capability.setFilesSharingPublicExpireDateEnabled(CapabilityBooleanType.fromValue(c.getInt(c
.getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED))));
capability.setFilesSharingPublicExpireDateDays(c.getInt(c
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/owncloud/android/db/ProviderMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
public class ProviderMeta {
public static final String DB_NAME = "filelist";
public static final int DB_VERSION = 46;
public static final int DB_VERSION = 47;

private ProviderMeta() {
// No instance
Expand Down Expand Up @@ -155,6 +155,8 @@ static public class ProviderTableMeta implements BaseColumns {
public static final String CAPABILITIES_SHARING_API_ENABLED = "sharing_api_enabled";
public static final String CAPABILITIES_SHARING_PUBLIC_ENABLED = "sharing_public_enabled";
public static final String CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED = "sharing_public_password_enforced";
public static final String CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD =
"sharing_public_ask_for_optional_password";
public static final String CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED =
"sharing_public_expire_date_enabled";
public static final String CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ private void createCapabilitiesTable(SQLiteDatabase db) {
+ ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST + TEXT
+ ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING + INTEGER
+ ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES + INTEGER
+ ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST + " TEXT );");
+ ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST + TEXT
+ ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD + " INTEGER );");
}

private void createUploadsTable(SQLiteDatabase db) {
Expand Down Expand Up @@ -1946,6 +1947,25 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (!upgraded) {
Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
}

if (oldVersion < 47 && newVersion >= 47) {
Log_OC.i(SQL, "Entering in the #47 add askForPassword to capability table");
db.beginTransaction();
try {
db.execSQL(ALTER_TABLE + ProviderTableMeta.CAPABILITIES_TABLE_NAME +
ADD_COLUMN + ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD +
" INTEGER ");

upgraded = true;
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}

if (!upgraded) {
Log_OC.i(SQL, String.format(Locale.ENGLISH, UPGRADE_VERSION_MSG, oldVersion, newVersion));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2016,10 +2016,13 @@ private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation ope
// Was tried without password, but not sure that it's optional.

// Try with password before giving up; see also ShareFileFragment#OnShareViaLinkListener
if (fileDetailFragment != null
&& fileDetailFragment.isAdded()) { // only if added to the view hierarchy!!
if (fileDetailFragment != null && fileDetailFragment.isAdded()) { // only if added to the view hierarchy

fileDetailFragment.getFileDetailSharingFragment().requestPasswordForShareViaLink(true);
fileDetailFragment
.getFileDetailSharingFragment()
.requestPasswordForShareViaLink(true,
getCapabilities().getFilesSharingPublicAskForOptionalPassword()
.isTrue());
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,11 @@ private void onCreateShareViaLinkOperationFinish(CreateShareViaLinkOperation ope

// Try with password before giving up; see also ShareFileFragment#OnShareViaLinkListener
ShareFileFragment shareFileFragment = getShareFileFragment();
if (shareFileFragment != null
&& shareFileFragment.isAdded()) { // only if added to the view hierarchy!!
if (shareFileFragment != null && shareFileFragment.isAdded()) { // only if added to the view hierarchy!!

shareFileFragment.requestPasswordForShareViaLink(true);
boolean askForPassword = getCapabilities().getFilesSharingPublicAskForOptionalPassword().isTrue();

shareFileFragment.requestPasswordForShareViaLink(true, askForPassword);
}

} else {
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/owncloud/android/ui/dialog/SendShareDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.status.OCCapability;
import com.owncloud.android.ui.activity.FileActivity;
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.adapter.SendButtonAdapter;
Expand All @@ -26,6 +27,8 @@
import com.owncloud.android.utils.MimeTypeUtil;
import com.owncloud.android.utils.ThemeUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -60,6 +63,7 @@ public class SendShareDialog extends BottomSheetDialogFragment {

private static final String KEY_OCFILE = "KEY_OCFILE";
private static final String KEY_SHARING_PUBLIC_PASSWORD_ENFORCED = "KEY_SHARING_PUBLIC_PASSWORD_ENFORCED";
private static final String KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD = "KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD";
private static final String KEY_HIDE_NCSHARING_OPTIONS = "KEY_HIDE_NCSHARING_OPTIONS";
private static final String TAG = SendShareDialog.class.getSimpleName();
public static final String PACKAGE_NAME = "PACKAGE_NAME";
Expand All @@ -69,16 +73,20 @@ public class SendShareDialog extends BottomSheetDialogFragment {
private OCFile file;
private boolean hideNcSharingOptions;
private boolean sharingPublicPasswordEnforced;
private boolean sharingPublicAskForPassword;
private FileOperationsHelper fileOperationsHelper;

public static SendShareDialog newInstance(OCFile file, boolean hideNcSharingOptions, boolean sharingPublicPasswordEnforced) {
public static SendShareDialog newInstance(OCFile file, boolean hideNcSharingOptions, OCCapability capability) {

SendShareDialog dialogFragment = new SendShareDialog();

Bundle args = new Bundle();
args.putParcelable(KEY_OCFILE, file);
args.putBoolean(KEY_HIDE_NCSHARING_OPTIONS, hideNcSharingOptions);
args.putBoolean(KEY_SHARING_PUBLIC_PASSWORD_ENFORCED, sharingPublicPasswordEnforced);
args.putBoolean(KEY_SHARING_PUBLIC_PASSWORD_ENFORCED,
capability.getFilesSharingPublicPasswordEnforced().isTrue());
args.putBoolean(KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD,
capability.getFilesSharingPublicAskForOptionalPassword().isTrue());
dialogFragment.setArguments(args);

return dialogFragment;
Expand All @@ -93,11 +101,14 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
file = getArguments().getParcelable(KEY_OCFILE);
hideNcSharingOptions = getArguments().getBoolean(KEY_HIDE_NCSHARING_OPTIONS, false);
sharingPublicPasswordEnforced = getArguments().getBoolean(KEY_SHARING_PUBLIC_PASSWORD_ENFORCED, false);
sharingPublicAskForPassword = getArguments().getBoolean(KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
public View onCreateView(@NotNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {

view = inflater.inflate(R.layout.send_share_fragment, container, false);

Expand Down Expand Up @@ -178,7 +189,9 @@ private void shareByLink() {
}

private void requestPasswordForShareViaLink() {
SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(file, true);
SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(file,
true,
sharingPublicAskForPassword);
dialog.show(getFragmentManager(), SharePasswordDialogFragment.PASSWORD_FRAGMENT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public class SharePasswordDialogFragment extends DialogFragment implements Dialo
private static final String ARG_FILE = "FILE";
private static final String ARG_SHARE = "SHARE";
private static final String ARG_CREATE_SHARE = "CREATE_SHARE";
private static final String ARG_ASK_FOR_PASSWORD = "ASK_FOR_PASSWORD";
public static final String PASSWORD_FRAGMENT = "PASSWORD_FRAGMENT";

private OCFile file;
private OCShare share;
private boolean createShare;
private boolean askForPassword;

@Override
public void onStart() {
Expand All @@ -81,11 +83,12 @@ public void onStart() {
* is bound to it.
* @return Dialog ready to show.
*/
public static SharePasswordDialogFragment newInstance(OCFile file, boolean createShare) {
public static SharePasswordDialogFragment newInstance(OCFile file, boolean createShare, boolean askForPassword) {
SharePasswordDialogFragment frag = new SharePasswordDialogFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_FILE, file);
args.putBoolean(ARG_CREATE_SHARE, createShare);
args.putBoolean(ARG_ASK_FOR_PASSWORD, askForPassword);
frag.setArguments(args);
return frag;
}
Expand Down Expand Up @@ -116,6 +119,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
file = getArguments().getParcelable(ARG_FILE);
share = getArguments().getParcelable(ARG_SHARE);
createShare = getArguments().getBoolean(ARG_CREATE_SHARE, false);
askForPassword = getArguments().getBoolean(ARG_ASK_FOR_PASSWORD, false);

// Inflate the layout for the dialog
LayoutInflater inflater = getActivity().getLayoutInflater();
Expand All @@ -130,14 +134,21 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
inputText.setText("");
inputText.requestFocus();

int title;
if (askForPassword) {
title = R.string.share_link_optional_password_title;
} else {
title = R.string.share_link_password_title;
}

// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
R.style.Theme_ownCloud_Dialog_NoButtonBarStyle);
builder.setView(v)
.setPositiveButton(R.string.common_ok, this)
.setNegativeButton(R.string.common_cancel, this)
.setNeutralButton(R.string.common_delete, this)
.setTitle(R.string.share_link_password_title);
.setTitle(title);
Dialog d = builder.create();

Window window = d.getWindow();
Expand All @@ -153,11 +164,9 @@ public void onClick(DialogInterface dialog, int which) {
if (which == AlertDialog.BUTTON_POSITIVE) {
String password = ((TextView) (getDialog().findViewById(R.id.share_password))).getText().toString();

if (TextUtils.isEmpty(password)) {
DisplayUtils.showSnackMessage(
getActivity().findViewById(android.R.id.content),
R.string.share_link_empty_password
);
if (!askForPassword && TextUtils.isEmpty(password)) {
DisplayUtils.showSnackMessage(getActivity().findViewById(android.R.id.content),
R.string.share_link_empty_password);
return;
}

Expand All @@ -172,6 +181,12 @@ public void onClick(DialogInterface dialog, int which) {
} else {
setPassword(share, null);
}
} else if (which == AlertDialog.BUTTON_NEGATIVE && askForPassword) {
if (share == null) {
setPassword(createShare, file, null);
} else {
setPassword(share, null);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,11 @@ public void toggleShareByLink() {
}

private void createShareLink() {
if (capabilities != null &&
capabilities.getFilesSharingPublicPasswordEnforced().isTrue()) {
if (capabilities != null && (capabilities.getFilesSharingPublicPasswordEnforced().isTrue() ||
capabilities.getFilesSharingPublicAskForOptionalPassword().isTrue())) {
// password enforced by server, request to the user before trying to create
requestPasswordForShareViaLink(true);
requestPasswordForShareViaLink(true,
capabilities.getFilesSharingPublicAskForOptionalPassword().isTrue());

} else {
// create without password if not enforced by server or we don't know if enforced;
Expand Down Expand Up @@ -417,7 +418,8 @@ private boolean optionsItemSelected(MenuItem item) {

return true;
case R.id.action_password: {
requestPasswordForShareViaLink(false);
requestPasswordForShareViaLink(false,
capabilities.getFilesSharingPublicAskForOptionalPassword().isTrue());
return true;
}
case R.id.action_share_expiration_date: {
Expand Down Expand Up @@ -527,12 +529,14 @@ public void updateNoteToShare(OCShare share, String note) {
/**
* Starts a dialog that requests a password to the user to protect a share link.
*
* @param createShare When 'true', the request for password will be followed by the creation of a new
* public link; when 'false', a public share is assumed to exist, and the password
* is bound to it.
* @param createShare When 'true', the request for password will be followed by the creation of a new public
* link; when 'false', a public share is assumed to exist, and the password is bound to it.
* @param askForPassword if true, password is optional
*/
public void requestPasswordForShareViaLink(boolean createShare) {
SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(file, createShare);
public void requestPasswordForShareViaLink(boolean createShare, boolean askForPassword) {
SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(file,
createShare,
askForPassword);
dialog.show(getChildFragmentManager(), SharePasswordDialogFragment.PASSWORD_FRAGMENT);
}

Expand Down
Loading

0 comments on commit b4f36f4

Please sign in to comment.