My code is:
if(mBooleanLoginDone==true)
{
mMenuItemProdType.setAccelerator(new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN, KeyCodeCombination.SHORTCUT_DOWN));
}
else
{
System.out.println("Not Loggged In");
}
In Above code mBooleanLoginDone is a Boolean variable indicating where the user logged in or not. I want shortcut key Ctrl+T to work only when user successfully logged in but right now I'm not logged in and shortcut key is also working. How can I solve it any idea?
Try
if(mBooleanLoginDone==true)
{
mMenuItemProdType.setAccelerator(new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN, KeyCodeCombination.SHORTCUT_DOWN));
}
else
{
mMenuItemProdType.setAccelerator(null);
System.out.println("Not Loggged In");
}
Related
binding.navView.setNavigationItemSelectedListener {
when(it.itemId){
R.id.requestWallpaper->{
val emailIntent=Intent().apply {
action=Intent.ACTION_SEND
data= Uri.parse("mailto:")
type="text/plain"
putExtra(Intent.EXTRA_EMAIL,"test#gmail.com")
putExtra(Intent.EXTRA_SUBJECT,"request wallpaper")
putExtra(Intent.EXTRA_TEXT,"request wallpaper")
}
if (emailIntent.resolveActivity(this!!.packageManager) !=null){
emailIntent.setPackage("com.google.android.gm")
startActivity(emailIntent)
}else{
Toast.makeText(this#MainActivity,"No app available to send email!!",Toast.LENGTH_SHORT).show()
}
}
When the navigation drawer opens, the user will want to make a wallpaper request and when he presses the imagebutton, I want him to send an e-mail to test#gmail.com via gmail for now, but test#gmail.com is not added directly to the "to" part of gmail.When I run it on the emulator, the email subject and e-mail text are added, but the recipient e-mail is not added, why?
You're so close here, the only thing that's missing is the Intent.EXTRA_EMAIL extra. That property is expecting an array of String values rather than a single String.
binding.navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.requestWallpaper -> {
val emailIntent = Intent().apply {
action = Intent.ACTION_SEND
data = Uri.parse("mailto:")
type = "text/plain"
putExtra(Intent.EXTRA_EMAIL, arrayOf("test#gmail.com"))
putExtra(Intent.EXTRA_SUBJECT, "request wallpaper")
putExtra(Intent.EXTRA_TEXT, "request wallpaper")
}
if (emailIntent.resolveActivity(this.packageManager) != null) {
emailIntent.setPackage("com.google.android.gm")
startActivity(emailIntent)
} else {
Toast.makeText(
this#MainActivity,
"No app available to send email!!",
Toast.LENGTH_SHORT
).show()
}
}
}
}
I don't know that you need the type="text/plain" property either, but it shouldn't hurt anything. Also, if you skip the setPackage step it'll allow the OS to ask which email app to use.
I want to test access to a method for a group of users with different roles in one test. I am trying to change the logged in user like this:
#Test
void allMenusAuthorizePermissions() throws Exception {
for (User user : ALL_ROLES_USERS) {
Authentication authentication = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), user.getAuthorities());
SecurityContextHolder.clearContext();
SecurityContextHolder.getContext().setAuthentication(authentication);
log.debug("User role: " + user.getAuthorities());
if (user == ADMIN || user == EDITOR) {
perform(get(MenuEditorController.MENU_EDITOR_URL).principal(authentication))
.andExpect(status().isOk());
}else{
perform(get(MenuEditorController.MENU_EDITOR_URL).principal(authentication))
.andExpect(status().isForbidden());
}
}
}
But no matter how hard I try, perform(get (...)) is always performed from the first user from the ALL_ROLES_USERS array. This can be seen from the log:
o.s.s.a.i.a.MethodSecurityInterceptor : Previously Authenticated: org.springframework.security.authentication.UsernamePasswordAuthenticationToken#e74255f0: Principal: +79990200001; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: CLIENT
-the same user in every iteration!
Any idea why this might be happening? Maybe perform(get(...)) is passing the JSESSIONID of the first user? I don't know where to dig anymore
You need to use:
#Test
void allMenusAuthorizePermissions() throws Exception {
for (User user : ALL_ROLES_USERS) {
log.debug("User role: " + user.getAuthorities());
if (user == ADMIN || user == EDITOR) {
// perform(get(MenuEditorController.MENU_EDITOR_URL).with(SecurityMockMvcRequestPostProcessors.user(user.getUsername()).authorities(user.getAuthorities())))
perform(get(MenuEditorController.MENU_EDITOR_URL).with(SecurityMockMvcRequestPostProcessors.user(user)))
.andExpect(status().isOk());
}else{
perform(get(MenuEditorController.MENU_EDITOR_URL).with(SecurityMockMvcRequestPostProcessors.user(user)))
.andExpect(status().isForbidden());
}
}
}
How to Mock the security context in Spring MVC for testing
i'm coding a Discord bot with JDA 4.1.1_101. I created the "ban" command, but i can't make the unban command work. I can't really understand why... Thank you for your help.
if (args[0].equalsIgnoreCase(Main.prefix + "unban")) {
if(event.getGuild().getSelfMember().hasPermission(Permission.BAN_MEMBERS)) {
if (args.length > 0 && args.length < 3) {
try {
event.getMessage().delete().queue();
User member = event.getMessage().getMentionedMembers().get(0).getUser();
String id = member.getId();
event.getGuild().unban(id).queue();
EmbedBuilder ban = new EmbedBuilder();
ban.setColor(Color.GREEN);
ban.setTitle("UnBan");
ban.setDescription("UnBan Report");
ban.addField("Staffer: ", event.getMessage().getAuthor().getName(), true);
ban.addField("Unban: ", member.getName(), true);
logs.sendMessage(ban.build()).queue();
} catch (IndexOutOfBoundsException exx) {
EmbedBuilder error = new EmbedBuilder();
error.setColor(0xff3923);
error.setTitle("Error: User");
error.setDescription("Invalid user.");
event.getChannel().sendMessage(error.build()).queue(message -> {
message.delete().queueAfter(5, TimeUnit.SECONDS);
});
}
} else {
EmbedBuilder error = new EmbedBuilder();
error.setColor(0xff3923);
error.setTitle("Error: Wrong usage.");
error.setDescription("Use: .unban [#user].");
event.getChannel().sendMessage(error.build()).queue(message -> {
message.delete().queueAfter(5, TimeUnit.SECONDS);
});
}
}
}
The problem is, that you are trying to retrieve the user from the mention in the message.
Since the user isn't part of the guild anymore, it seems like this doesn't work.
In order to work around this issue, you have to retrieve the ID manually.
A mention is always in the format <#userid> or <!#userid>.
To get the ID you could just split the message and replace the unnecessary parts, e.g. String id = event.getMessage().getContentRaw().split("<")[1].split(">")[0].replace("!", "").replace("#", "");
I'm sure there are better and smoother ways for doing this. ;)
A better way of retrieving the ID would be using a regex such as <#!?(\d+)> as mentioned by Minn.
In order to get the name of the user, you just need the ID via event.getJDA().getUserById(id).getName().
It's important to mention that you can't properly mention a user who isn't on the server (which is the case when they are banned).
(Addition: I tried using .getMentionedUsers() with the same result as OP.)
This is how i am deleting a record,can you please suggest me what is the best approach to delete a record.
public function delete_post($id) {
//Check if id is numeric and exists
if( (is_numeric($id)) && (!empty($id)) )
{
$post = Post::find($id);
// check if this id belongs to user (User has author)
if(Auth::id() == $post->user_id){
Post::with('likes')->whereId($id)->delete();
}else{
Session::flash('error', 'You can't delete this.
}
}else{
Session::flash('error', 'Problem with your input');
}
}
You should put your delete into a transactions
More in here:
Laravel Transactions!
I am using auth module of kohana. I did register and login and its working fine. But when i do Auth::instance()->get_user()->id i get NULL
While login i do it with Auth::instance()->login($validator['email'], $validator['password']) and then redirect user to home page.
But when in one of the controller i do Auth::instance()->get_user()->id i get NULL
What would be the cause. Is that i have to first set something???
Try Auth::instance()->get_user()->pk().
pk() is for primary key.
Works in KO3.
My Mistake
In the _login function of modules/auth/classes/kohana/auth/orm.php
In that i was doing the following
$user = ORM::factory('user');
$user->where('email', ' = ', $email)
->and_where('password', ' = ', $password)
->find();
// TODO remember to be done
if ($user !== null) {
$this->complete_login($user);
return true;
} else {
return false;
}
In above i was checking $user is null or not but if the email and password not match the user instance will be created with NULL values for all the columns.
So now i am checking $user->id !== NULL and it is working fine.
Try this:
if ($user->loaded()) {
$this->complete_login($user);
return true;
} else {
return false;
}
See ORM::__call() if you want to know what happends (since ORM::loaded() does not exist)